Reputation: 533
I have a listview in C sharp windows forms application like in the following. The following code of lines are there in Form_load() method .
But the issue is it does not start inserting a row from first column. Instead , it starts from the second column. Because of that value passed to the last column is always lost.
listView1.Columns.Add("ColumnOne", 150);
listView1.Columns.Add("ColumnTwo", 150);
listView1.Columns.Add("ColumnThree", 150);
listView1.Columns.Add("ColumnFour", 150);
listView1.Columns.Add("ColumnFive", 150);
listView1.Columns.Add("Column6", 150);
foreach (MyClass et in _myData)
{
ListViewItem lt = new ListViewItem();
lt.SubItems.Add(et.DataOne.ToString());
lt.SubItems.Add(et.DataTwo.ToString());
lt.SubItems.Add(et.DataThree.ToString());
lt.SubItems.Add(et.DataFour.ToLongDateString());
lt.SubItems.Add(et.DataFive);
lt.SubItems.Add(et.DataSix);
}
I tried everything possible. I don't even see that I have made a mistake. Can someone pls give me a solution to this ?
Upvotes: 2
Views: 54
Reputation: 6385
The ListView
is a bit clunky when it comes to the different columns. What is shown in the first column in Detail
view is actually the text of the ListViewItem
itself and not the first SubItem
. So what you want to do is start assigning DataTwo
and so on to the subgroups
and DataOne
to the ListViewItem.Text
:
listView1.Columns.Add("ColumnOne", 150);
listView1.Columns.Add("ColumnTwo", 150);
listView1.Columns.Add("ColumnThree", 150);
listView1.Columns.Add("ColumnFour", 150);
listView1.Columns.Add("ColumnFive", 150);
listView1.Columns.Add("Column6", 150);
foreach (MyClass et in _myData)
{
ListViewItem lt = new ListViewItem(et.DataOne.ToString());
lt.SubItems.Add(et.DataTwo.ToString());
lt.SubItems.Add(et.DataThree.ToString());
lt.SubItems.Add(et.DataFour.ToLongDateString());
lt.SubItems.Add(et.DataFive);
lt.SubItems.Add(et.DataSix);
// I added this to your code
listView1.Items.Add(lt);
}
Upvotes: 3