I create a listview and the number of columns are determined at runtime. I have been reading texts on the web all over about listview( and I still am) but I wish to know how to add items to a specific column in listview I thought something like:
m_listview.Items.Add("1850").SubItems.Add("yes");
would work assuming the "1850" which is the text of the column would be the target column.
ListViewItems aren't aware of your ListView columns.
In order to directly reference the column, you first need to add all the columns to the ListViewItem.
So... lets say your ListView has three columns A, B and C; This next bit of code will only add data to column A:
To get it to add text to column C as well, we first need to tell it it has a column B...
So now we'll have columns A, B and C in the ListViewItem, and text appearing in the A and C columns, but not the B.
Finally... now that we HAVE told it it has three columns, we can do whatever we like to those specific columns...
hope that helps!
I'm usially inherit ListViewItem as :
where
using
Something like this maybe?
Index 0 is the main item. Index 1 > are the subitems.
This is what I do because the columns are added at run time via the users preference.
I am using a listView to display information from a database to the user. ( I probably should use DataGrid, but when I first made the program DataGrid didn't have built in full row select and I was too big of a newb to go through the custom control examples I found)
The user selects which columns they want to show up out of the total, how wide, and the title text.
I created a user preferences table that saved information about the columns to add
I also made a class
ColumnPreferences
and added a property for each of those columnsUsed DataReader to make
List<ColumnPreferences>
I sort this list according to display index, so I can iterate through it in the order the columns are displayed.
I do an if statement checking if the next preference in preferences is visible.
If it is then I add the column using
ListView1.Columns.Add(ColumnName, ColumnText, ColumnWidth);
At this point the columns are all set, to save where the user drags them around to I do basically the opposite. I iterate through all column in Listview.columns and save the column display index and column width back into my user preference table
When loading the actual data from the table I use this same list of column preferences to check whether I should display the data.
it is again sorted by display index, I check if it is visible, if it is not I skip it and go to the next one
if it is visible I use
myDataReader[ ColumnPreference.ColumnName ]
to look up the approprate ordinal for the data I want.I just add each of those results, in order, to a list view item / subitems then add them to the list view
ListViews don't support databinding and strongly typed naming. I would recommend considering using a DataGridView instead. Depending on what you need, it may save yourself some sanity points.
Chernikov has it nicely implemented to make things a bit saner.