Reputation: 45
My intent is to have a simple conversion of feet to meters. Could you provide helpful suggestions to populate the 'meters' column? The following code was written to show two columns in listview in Visual Studio 2008 C++:
//Show details
listView1->View = View::Details;
//Allow the user to change text
listView1->LabelEdit = true;
listView1->GridLines = true;
//Add 2 columns and 2 rows
listView1->Columns->Insert(0,"Feet",150, HorizontalAlignment::Center);
listView1->Items->Insert(0, "50");
listView1->Items->Insert(1, "20");
listView1->Columns->Insert(1,"Meters", 150, HorizontalAlignment::Center);
listView1->Items->Insert(0, "15.24");
listView1->Items->Insert(1, "6.10");
The incorrect output in listView1 is:
Feet Meters
15.24
6.10
50
20
What the output should look like in listView1:
Feet Meters
50 15.24
20 6.10
I am sure that I am missing something simple. Thanks for any and all help :)
Upvotes: 2
Views: 4248
Reputation: 886
Listview in Details mode maps the columns as subitems for each item. First column shows the item text itself and the other use its subitems. So the code could be:
listview1->Columns->Add("Feet", 150, HorizontalAlignment::Center);
listview1->Columns->Add("Meters", 150, HorizontalAlignment::Center);
listview1->Items->Add("50"); // this will be shown in column Feet
listview1->Items[0]->SubItems->Add("20"); // this will be shown in column Meters
listview1->Items->Add("15.24");
listview1->Items[1]->SubItems->Add("6.10");
NOTE: I have used "Add" method instead "Insert" because, in this case, the order for items and columns don't care. I think it is more convenient than "Insert".
I hope this help you.
Upvotes: 3