[RESOLVED] ListView.DataSource = DataTable
How can I bind a Datatable to a ListView with Details? Something similar to how it works with the ListBox:
Code:
listView.DataSource = myDataTable;
listView.Columns[0].DisplayMember[0] = "Name"; //DisplayMember for the first column
listView.Columns[0].DisplayMember[1] = "Age"; //DisplayMember for the second column
listView.ValueMember = "Essay"; //The ValueMember for the row
Does it exist anything similar to what I have written above?
Re: ListView.DataSource = DataTable
If you're talking about WinForms then no, the ListView doesn't support data-binding. That's why you should use a DataGridView unless you specifically need the ListView-specific features, i.e. multiple views and grouping.
Re: ListView.DataSource = DataTable
Okay, I added a DataGridView instead. I also changed some colours and properties so it should work more like a listview.
My datatable has this design:
Column1 = string, Column2 = string, Column3 = myOwnType
I want to display column 1 in column 1 and the same with column 2. I don't want to display column3, it should only be accessible from the "SelectedRow" property or something.
How do make it so?
Secondly, how can I hide this:
http://i54.tinypic.com/2gtyp3p.png
(The one with a red cross)
Re: ListView.DataSource = DataTable
If you don't want every column from the data source displayed in the grid then you basically have two choices:
1. After binding the grid, hide the column you don;t want shown.
2. Add only the columns you want in the designer and set their DataPropertyName for binding. You then set AutoGenerateColumns to False before binding.
As for the last question, you shouldn't even need to ask it. When you're using a new control, the very first thing you should do is look in the Properties window to see what it can do. If you've done that then you have your answer.
Re: ListView.DataSource = DataTable
Okay I will hide the column.
I always look what it can do...but I didn't know the frikkin name of the thing I want to hide?? :confused:
But fine. I googled it and solved it by setting "RowHeaders" to not visible :)
Thank you for your help!