I have one listview whith several columns. I want to fill this listview in a vertical form column to column.
Sorry, but this is not (easily) possible.
A ListView
has a list of ListViewItem
s, where each one has a List of ListViewSubItem
s (and to make it a little more complex at the first spot, the first ListViewSubItem
is the same as the ListViewItem
itself).
So if you like to fill up a ListView
column by column you first have to add the ListViewItem
s to the ListView
for all the values you want in the first column.
Afterwards you iterate through the ListView.Items
and call on every ListViewItem.Subitems.Add
to fill up the next column. This must be done for each column you like to fill.
If you like to fill in the column values in another order then from left to right, you should take a look into the DisplayIndex
of the ColumnHeader
within the ListView.Columns
.
Some example code:
// Some values
var someValues = Enumerable.Range(1, 10);
// Fill up the first column
foreach (var item in someValues)
{
listView.Items.Add("0." + item);
}
// Run for each column in the listView (the first is already filled up)
foreach (ColumnHeader column in listView.Columns.Cast<ColumnHeader>().Skip(1))
{
// Get the value and the index for which row the value should be
foreach (var item in someValues.Select((Value, Index) => new { Value, Index }))
{
// Add the value to the given row, thous leading to be added as new column
listView.Items[item.Index].SubItems.Add(column.Index + "." + item.Value);
}
}