Search code examples
monotreeviewgtk#gtk2

gtk#: remove a column by its column name


I have a TreeView in GTK# and want to remove specific columns. How can I achieve this?

TreeView.RemoveColumn() sounds good, but I've got no idea how to find the desired columns by their names.

Thinking of something like TreeView.RemoveColumn( TreeView.FindColumn("address"));

I really have no idea... :'-(


Solution

  • You can do a sequential search over the columns, this way:

        /// <summary>
        /// Finds a column by its title.
        /// </summary>
        /// <returns>The first <see cref="Gtk.TreeViewColumn"/> with that title.</returns>
        /// <param name="tv">The given <see cref="Gtk.TreeView"/>.</param>
        /// <param name="title">The title to look for.</param>
        static Gtk.TreeViewColumn FindColumnByTitle(Gtk.TreeView tv, string title)
        {
            Gtk.TreeViewColumn toret = null;
    
            title = title.ToLower();
            foreach(Gtk.TreeViewColumn column in tv.Columns) {
                if ( column.Title.ToLower() == title ) {
                    toret = column; 
                    break;
                }
            }
    
            return toret;
        }
    

    Hope this helps.