|
-
Jun 28th, 2011, 11:50 AM
#1
Thread Starter
Hyperactive Member
[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?
-
Jun 28th, 2011, 07:21 PM
#2
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.
-
Jun 29th, 2011, 03:54 AM
#3
Thread Starter
Hyperactive Member
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:

(The one with a red cross)
-
Jun 29th, 2011, 04:29 AM
#4
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.
-
Jun 29th, 2011, 05:23 AM
#5
Thread Starter
Hyperactive Member
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?? 
But fine. I googled it and solved it by setting "RowHeaders" to not visible 
Thank you for your help!
Last edited by Cyb3rH4Xter; Jun 29th, 2011 at 05:30 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|