[Gtk-sharp-list] How to Get Row Values from a TreeView

Philip Van Hoof spam at pvanhoof.be
Thu Feb 14 07:10:16 EST 2008


On Thu, 2008-02-14 at 15:27 +0800, Marc Glenn wrote:
> Hello everyone,
> 
>      Does anyone know how to retrieve values currently displayed on a 
> TreeView widget?
> 
>      Suppose a row entry from the TreeView was clicked, how can the the 
> data in the row be retrieved?
> 
>      I checked the Mono Documentation but I can't find a method to do this.

There are a few ways, but this is one possibility:

treeview.Selection.Changed += OnSelectionChanged;

private void OnSelectionChanged; (object o, EventArgs args)
{
	Gtk.TreeSelection selection = o as Gtk.TreeSelection;
	YourColumn'sType data;
	Gtk.TreeModel model;
	Gtk.TreeIter iter;

	if (selection.GetSelected (out model, out iter)) {
		int column = YourColumnNumber;
		GLib.Value val = GLib.Value.Empty;
		model.GetValue (iter, column, ref val);
		data = (YourColumn'sType) val.Val;
		val.Dispose ();
	}
}

Advise: check out the Model View Controller pattern in detail.

Change "YourColumn'sType" into "string" in case the data is a string.


-- 
Philip Van Hoof, freelance software developer
home: me at pvanhoof dot be 
gnome: pvanhoof at gnome dot org 
http://pvanhoof.be/blog
http://codeminded.be



More information about the Gtk-sharp-list mailing list