Search code examples
c#monomonodevelopgtk#

How to loop trought nodeview rows in gtk#


I have nodeview with 2 columns and need to loop trough values in column a, and value in cell match an condition, i need to ask user what to do, and write user input in column b.

I have tried with

for (int i = 0; i < nodeview1.Model.IterNChildren(); i++)

    {
        //var x = nodeview1.Columns [0].ToString ();
        //var x = nodeview1.Columns [i].ToString ();
        //var x = nodeview1.Columns [0].Data.Values.ToString ();
    }

but can't get any value from cell...

Can someone help with this?


Solution

  • This is because you are iterating on the view while to obtain values you should iterate on the model. To iterate on model you can use code as follows:

    Gtk.TreeModel model = nodeview1.Model;
    Gtk.TreeIter iter;
    if (model.GetIterFirst(out iter)) {
        do {
            Console.WriteLine("COLUMN 1: " +  model.GetValue(iter, 0));
            Console.WriteLine("COLUMN 2: " +  model.GetValue(iter, 1));
        } while (model.IterNext(ref iter));
    }
    

    The if is needed because GetIterFirst returns false if the model is empty. Then you use it and loop untill IterNext returns false.

    Also note how you can use GetValue on the model to get the value corresponding to the row "pointer" by the Gtk.TreeIter and the column (index starts from 0).