Results 1 to 5 of 5

Thread: [RESOLVED] ListView.DataSource = DataTable

  1. #1

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Resolved [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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    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)

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    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
  •  



Click Here to Expand Forum to Full Width