ListView Control
-
hello, i'm trying to do 2 things with this control. the first is add a value to a sub-column of a row(or officially stated, the sub item to the item). the catch is, i can add the item to the row if its Selected. such as: listView.Items[listView.SelectedItems[0].Index].SubItems[1].Text = this.textBox_Value.Text; if i try to add the value without a row being selected it crashes the control. if i try: item.SubItems.Add(this.textBox_Value.Text); this.listView_Parameters.Items[0] = items; it just adds it to the second column of a new row. so how do i add a value to a particular row/sub-column without selecting that row? the second, i've also been trying to show a row as being Selected with the blue line. ideally i'd like to put a value in the control and show it as if its been selected. any advice would be greatly appreciated. i'm probably missing something obvious...:wtf: thank you, Orion
-
hello, i'm trying to do 2 things with this control. the first is add a value to a sub-column of a row(or officially stated, the sub item to the item). the catch is, i can add the item to the row if its Selected. such as: listView.Items[listView.SelectedItems[0].Index].SubItems[1].Text = this.textBox_Value.Text; if i try to add the value without a row being selected it crashes the control. if i try: item.SubItems.Add(this.textBox_Value.Text); this.listView_Parameters.Items[0] = items; it just adds it to the second column of a new row. so how do i add a value to a particular row/sub-column without selecting that row? the second, i've also been trying to show a row as being Selected with the blue line. ideally i'd like to put a value in the control and show it as if its been selected. any advice would be greatly appreciated. i'm probably missing something obvious...:wtf: thank you, Orion
look at your code. If you use SelectedItems property to get an index value for your listview, and no item is selected there wont be a value in SelectedItems[0], thats why it crashes. // Add a new item and at subcolumn int index = Items.Count; ListViewItem item = new ListViewItem("Column Text,index) item.SubItems.Add("Subcolumn Text"); Items.Add(item); // Add only a subitem to an existing item listView.Items[index].SubItems.Add("subcolumn Text"); you can show a blue line by first adding the item to the row then use the controls SelectedItem property to select the index of the new item. You might also want to make sure that fullrowselect is set to true. Brandon Parker
-
look at your code. If you use SelectedItems property to get an index value for your listview, and no item is selected there wont be a value in SelectedItems[0], thats why it crashes. // Add a new item and at subcolumn int index = Items.Count; ListViewItem item = new ListViewItem("Column Text,index) item.SubItems.Add("Subcolumn Text"); Items.Add(item); // Add only a subitem to an existing item listView.Items[index].SubItems.Add("subcolumn Text"); you can show a blue line by first adding the item to the row then use the controls SelectedItem property to select the index of the new item. You might also want to make sure that fullrowselect is set to true. Brandon Parker
thanks Brandon, actually a few minutes after i posted it i did get the first part. i did it a little different though. listView.Items[0].SubItems[1].Text = this.textBox_Value.Text; as for showing the blue line, i want to show it before any sub item has been added. to let the user know the first row has been auto selected. i've got the FullRowSelect prop set to true, no problem. if i do this.listview.SelectedItems[0] = true;//it complains Orion
-
thanks Brandon, actually a few minutes after i posted it i did get the first part. i did it a little different though. listView.Items[0].SubItems[1].Text = this.textBox_Value.Text; as for showing the blue line, i want to show it before any sub item has been added. to let the user know the first row has been auto selected. i've got the FullRowSelect prop set to true, no problem. if i do this.listview.SelectedItems[0] = true;//it complains Orion
try this instead. I havent tried it so im not sure it will work,so let me know. Also remember, if you use the SelectedItems Property and no item is selected then it will be null. so it will complain. this.listView.Items[0].Selected = true;
-
try this instead. I havent tried it so im not sure it will work,so let me know. Also remember, if you use the SelectedItems Property and no item is selected then it will be null. so it will complain. this.listView.Items[0].Selected = true;
it sort of works, but now the value i'm adding to the sub item/second column doesn't go in... back to the drawing board and thanks:) Orion