[Gtk-sharp-list] Sorting a TreeView with a filter

Adam Tauno Williams awilliam at whitemice.org
Fri Sep 12 08:13:46 EDT 2008

I have a rather complicated TreeView using a ListStore.  Filtering the
view works,  but I'm so far unable to get sorting to work.  I have a
sort object connected to the model and the filter object connected to
the sort object;  I'm found very similair constructs in other projects
(including Galaxium
<http://code.google.com/p/galaxium/source/browse/trunk/src/Galaxium.Gui.Gtk/NewTreeView/GalaxiumTreeView.cs?r=551>) but it just doesn't sort.  My column-header-click happens when a column gets clicked but it doesn't sort,  the actual sort method never gets pinged.  Despite beng connect-before Gtk seems to be doing something before my column-header-clicked gets called.   Any pointers appreciated. 

What happens when a column header is clicked:
(Whitemice.Consonance:20431): Gtk-CRITICAL **:
gtk_tree_sortable_get_sort_column_id: assertion `GTK_IS_TREE_SORTABLE
(sortable)' failed
(Whitemice.Consonance:20431): Gtk-CRITICAL **:
gtk_tree_sortable_has_default_sort_func: assertion `GTK_IS_TREE_SORTABLE
(sortable)' failed
(Whitemice.Consonance:20431): Gtk-CRITICAL **:
gtk_tree_sortable_set_sort_column_id: assertion `GTK_IS_TREE_SORTABLE
(sortable)' failed
Sort Column:4 Order:Ascending

The actual sort method never gets called


      TreeView view;
      ListStore taskStore;
      TreeModelSort sortAdapter;
      TreeModelFilter  taskFilter;
		  int             sortColumn;
		  SortType        sortOrder;

			taskStore = new Gtk.ListStore();
			objectIdColumn = new Gtk.TreeViewColumn ();
			objectIdColumn.Title = "Object Id";
			objectIdColumn.Reorderable = true;
			objectIdColumn.Resizable = true;
			objectIdColumn.Clickable = true;
			cell = new Gtk.CellRendererText ();
			objectIdColumn.PackStart(cell, true);
			objectIdColumn.SetCellDataFunc(cell, new
			objectIdColumn.SortColumnId = 0;
			objectIdColumn.Clicked += new EventHandler(ColumnClicked);
		  sortAdapter = new TreeModelSort(taskStore);
		  sortAdapter.SetSortFunc(-2, OrderTasks);
			taskFilter = new TreeModelFilter(sortAdapter, null);
			taskFilter.VisibleFunc = new
			view.Model = taskFilter;

		protected void ColumnClicked(object sender, EventArgs args)
		  TreeViewColumn column;

		  if (sender is Gtk.TreeViewColumn)
		    column = (sender as Gtk.TreeViewColumn);
		    if (column.SortColumnId == sortColumn)
		      if (sortOrder == SortType.Ascending)
		        sortOrder = SortType.Descending;
		      else sortOrder = SortType.Ascending;
		      column.SortOrder = sortOrder;
		      column.SortIndicator = true;
		    } else 
		        sortColumn = column.SortColumnId;
		        sortOrder = SortType.Ascending;
		        column.SortIndicator = true;
		    Console.WriteLine("Sort Column:{0} Order:{1}", sortColumn,
		    sortAdapter.SetSortColumnId(sortColumn, sortOrder);
		    sortAdapter.ChangeSortColumn ();
		} // end ColumnClicked
protected int OrderTasks(TreeModel model, TreeIter t1, TreeIter t2)
//  ObjectId	0   Title 1     Project	2   Start	3
//  Creator	4   Status 5 Priority 6 End 7
//  Executant	8 Completed	9
 return 0;

More information about the Gtk-sharp-list mailing list