I have a listview column where I have added a button (to a column of type control). The button has a handler attached to it, and I need to reference the row the button is in. How can I do this?
I cannot use lst.SelectedItem.SubItems(1).Text because the row may not be selected, only the button in it clicked.
Hi there, a simple way is: when adding the button to the ListViewItem set the ListViewItem as the Tag property of the button.
ListViewItem itmX = new ListViewItem("Hello");
Button btn = new Button();
btn.Text = "World";
btn.Tag = itmX;
itmX.SubItems.Add(btn);
OK, I can store the object in the tag. But how can I use that to remove the row, for example. I know I can remove a row like this: lst.Items(x).Remove() ... where x is the index... but how do I use that object in the tag to remove the row?
in your button_click event
Button btn = sender as Button;
LsitViewItem itmX = btn.Tag as ListViewItem
then any of the following Remove methods:
lst.Items[itmX.Index].Remove();
lst.Items.Remove(itmX);
lst.Items.RemoveAt(itmX.Index);
Cheers james
Thanx!