I am trying to populate the subItems of a listView
after the items have already been added to the view itself. This has been throwing an error, and I do not know if it's because I am doing it wrong or if adding to a subitem after it has been created isn't possible.
I have created a windows form with a listView
on it very imaginatively named listView1
, which is populated with components and the possible hash functions that can be performed on them.
This works fine and is populated as I would like it to. Now the user clicks a button that is supposed to run those hash functions on each component and populate the appropriate box to the right of each as it is done.
I cannot seem to get each box to fill out. My code:
//Put the verification in the appropriate column, and line
//Iterate through each row until the appropriate one is found
for(int j=0;j<this->listView1->Items->Count;j++){
//When the row matches the component that was just verified, add things to that row
if(this->listView1->Items[j]->Text == response->componentID){
//Now that the row has been successfully found, find the column
for(int k=1;k<this->listView1->Columns->Count;k++){
//When the column matches the algorithm that was just ran, add the result
if(this->listView1->Columns[k]->Text == response->algorithmType.ToString()){
//****The line giving me trouble
this->listView1->Items[j]->SubItems[k]->Text = response->verifyResult;
//****End of troublesome line
}
}
}
}
Running it as is results in the following popup
Do I just need to create all of the items again and then clear out the listView1
before adding the "new" items to it? Or is this code salvagable?
ListView.SubItems
collection is empty by default. You should first add sub items using for example Add
method of sub-items collection. Then after you added some sub items then you can change text of sub-item using its index when you need.
this->listView1->Items[i]->SubItems->Add("Some text for SubItem");
Also when working on subitems in the collection, use 1-based index for sub-items because index 0 is the item which owns the collection.
this->listView1->Items[i]->SubItems[1]->Text = "Some new text";