Reputation: 165
I'm a beginner in C# and I am trying to create a simple view, where I have data listed into two separate columns in a ListView
. I have created the columns, and added subItems to two different ListViewItems
. However, when compiling only the main item shows up, an no SubItems
are present.
listView1.Columns.Add("Client Code");
listView1.Columns.Add("Client Name");
ListViewItem item = new ListViewItem("Client Code");
ListViewItem item2 = new ListViewItem("Client Name");
listView1.Items.AddRange(new ListViewItem[] {item, item2});
foreach(string clientCode in clientCodes)
//clientCode is a list of Strings initialized earlier in the code
{
listView1.Items[0].SubItems.Add(code);
}
I have also tried the following, with no success:
ListViewItem item = new ListViewItem("Client Code");
ListViewItem item2 = new ListViewItem("Client Name");
foreach(string clientCode in clientCodes)
//clientCode is a list of Strings initialized earlier in the code
{
item.SubItems.Add(code);
}
listView1.Items.AddRange(new ListViewItem[] {item, item2});
Simply adding
item.SubItems.Add("test");
or
listView1.Items[0].SubItems.Add("test");
also has no effect.
The output of the code above is two columns, one with "Client Code" and one with "Client Name" but no additional data.
What am I doing wrong, why isn't any of the data showing up in the columns?
Any help is greatly appreciated. Thank you.
Upvotes: 4
Views: 3430
Reputation:
We need to set the View property of ListView as View.Details before adding items to display the items as shown below.
listView1.View = View.Details;
listView1.Columns.Add("Client Code");
listView1.Columns.Add("Client Name");
Upvotes: 5