Treeview, delete the selected row (can't get it done :( )

View: New views
10 Messages — Rating Filter:   Alert me  

Treeview, delete the selected row (can't get it done :( )

by Werner666 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

I have a treeview on my form.
I wan't something very simple but just can't get it done. :(

After clicking 1 button the selected row in my treeview has to be deleted.

This is my code so far:

[code]
TreeIter iter;
TreeModel model;
               
               
TreePath path = ((TreeSelection)sender).GetSelectedRows();
this.klantListStore.GetIter(out iter, path[0]);
Console.WriteLine(path[0]);
                       
klantListStore.Remove(ref iter);
[/code]

But if I compile this and press the delete button I get:

System.InvalidCastException: Cannot cast from source type to destination type.
  at MainWindow.OnButton2Clicked (System.Object sender, System.EventArgs e) [0x00000]

...

Does anybody know what I do wrong?

Re: Treeview, delete the selected row (can't get it done :( )

by countcb :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Werner666 wrote:
[code]
TreeIter iter;
TreeModel model;
               
               
TreePath path = ((TreeSelection)sender).GetSelectedRows();
this.klantListStore.GetIter(out iter, path[0]);
Console.WriteLine(path[0]);
                       
klantListStore.Remove(ref iter);
[/code]
The Exception is in the OnButton2Clicked Method.
Is the code you posted above inside this OnButton2Clicked Method?
If yes, sender is the button you clicked on. So you tried to cast a button to a TreeSelection. That's why you get a InvalidCastException.

Re: Treeview, delete the selected row (can't get it done :( )

by Werner666 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ok, thanks.
And this code is in the button2clicked event indeed.

I decided to do is this way:
.. onSelectionChangedEvent ...
((TreeSelection)o).GetSelected(out iterSelected);
//iterSelected is an public var inside the class, not declared inside a method. :) And its a TreeIter yes. ;)

Than in button2Clicked
listStore.Remove(ref iterSelected);

Well... my application doesn't crash anymore. :D
But now I get this error when clicking the delete button:

(CustomerAdministartor:3579): Gtk-CRITICAL **: gtk_tree_store_remove: assertion `VALID_ITER (iter, tree_store)' failed

I don't really understand what this means... :( Except the part that he failed to delete the row I selected. :P


countcb wrote:
Werner666 wrote:
[code]
TreeIter iter;
TreeModel model;
               
               
TreePath path = ((TreeSelection)sender).GetSelectedRows();
this.klantListStore.GetIter(out iter, path[0]);
Console.WriteLine(path[0]);
                       
klantListStore.Remove(ref iter);
[/code]
The Exception is in the OnButton2Clicked Method.
Is the code you posted above inside this OnButton2Clicked Method?
If yes, sender is the button you clicked on. So you tried to cast a button to a TreeSelection. That's why you get a InvalidCastException.

Re: Treeview, delete the selected row (can't get it done :( )

by countcb :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The error message means, that the passed iterator is not valid for your listStore.
You can check this yourself with
listStore.IterIsValid(iterSelected);

It will return true if the iter is valid, false otherwise.

WHY it is not valid I can't see with the code you showd.
Can you show the line where you connect the OnSelectionChanged Method with the Changed Event?

Re: Treeview, delete the selected row (can't get it done :( )

by Werner666 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

That's this line:

tv_klanten.Selection.Changed += OnSelectionChanged;

tv_klanten is a TreeView widget.


countcb wrote:
The error message means, that the passed iterator is not valid for your listStore.
You can check this yourself with
listStore.IterIsValid(iterSelected);

It will return true if the iter is valid, false otherwise.

WHY it is not valid I can't see with the code you showd.
Can you show the line where you connect the OnSelectionChanged Method with the Changed Event?

Re: Treeview, delete the selected row (can't get it done :( )

by countcb :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The only problem i can imagine is that your assinging a TreeModelFilter or a TreeModelsort to tv_klanten.Model.

Then the selction you get when you click on the tree will be an iterator for THAT model. NOT for your listStore. Thats why your iter is not valid.

If you do like I assume you have to change the call

listStore.Remove(ref iterSelected);
to
listStore.Remove(ref subModel.ConvertIterToChildIter(iterSelected));

where "subModel" is the model you assigned to tv_klanten.Model

Re: Treeview, delete the selected row (can't get it done :( )

by Werner666 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ok thanks. I guess that's the problem. I use a TreeModelFilter.
So I changed it to this:

klantListStore.Remove(ref filter.ConvertIterToChildIter(iterSelected));

But when I try to compile I get:

A ref or out argument must be an assignable variable(CS1510)

However, the treeModelFilter called filter is public...


countcb wrote:
The only problem i can imagine is that your assinging a TreeModelFilter or a TreeModelsort to tv_klanten.Model.

Then the selction you get when you click on the tree will be an iterator for THAT model. NOT for your listStore. Thats why your iter is not valid.

If you do like I assume you have to change the call

listStore.Remove(ref iterSelected);
to
listStore.Remove(ref subModel.ConvertIterToChildIter(iterSelected));

where "subModel" is the model you assigned to tv_klanten.Model

Re: Treeview, delete the selected row (can't get it done :( )

by countcb :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ah. sorry. My mistake.

The parameter to the remove method is passed via the ref keyword.
That means the Remove method wants to change this iter (it will set the iter to the next valid row after it deleted the row).

But we just passed a method which will return a iter. But the remove method can't assign anything to this reutn value because it not held in a variable.

So the correct way of doing it would be:

TreeIter iter = subModel.ConvertIterToChildIter(iterSelected);
listStore.Remove(ref iter);

Re: Treeview, delete the selected row (can't get it done :( )

by Werner666 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


countcb wrote:
Ah. sorry. My mistake.

The parameter to the remove method is passed via the ref keyword.
That means the Remove method wants to change this iter (it will set the iter to the next valid row after it deleted the row).

But we just passed a method which will return a iter. But the remove method can't assign anything to this reutn value because it not held in a variable.

So the correct way of doing it would be:

TreeIter iter = subModel.ConvertIterToChildIter(iterSelected);
listStore.Remove(ref iter);
Already tried that but than I get 3 error messages after clicking the delete button.
Can't remember what messages but I just deleted the whole treeModelFilter. Adding new rows to the treeview didn't work either with the treemodelfilter.
Everything works now except the searchfunction but I'll found something else to get the search option working. :-)


Re: Treeview, delete the selected row (can't get it done :( )

by Alessandro75 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi!

the method return an array of TreePath... so...

TreePath[] path = ((TreeSelection)sender).GetSelectedRows();

test it.



Werner666 wrote:
Hello,

I have a treeview on my form.
I wan't something very simple but just can't get it done. :(

After clicking 1 button the selected row in my treeview has to be deleted.

This is my code so far:84bnp

[code]
TreeIter iter;
TreeModel model;
               
               
TreePath path = ((TreeSelection)sender).GetSelectedRows();
this.klantListStore.GetIter(out iter, path[0]);
Console.WriteLine(path[0]);
                       
klantListStore.Remove(ref iter);
[/code]

But if I compile this and press the delete button I get:

System.InvalidCastException: Cannot cast from source type to destination type.
  at MainWindow.OnButton2Clicked (System.Object sender, System.EventArgs e) [0x00000]

...

Does anybody know what I do wrong?