Results 1 to 2 of 2

Thread: [2008] DataGridView Control Binding with Select

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    [2008] DataGridView Control Binding with Select

    When I run this:

    DG1.DataSource = dtU

    I get the result I expect.

    However when I run this:

    DG1.DataSource = dtU.Select("CaseNumber = 0")

    I get the right number of rows, but the following columns (non existent in the database):

    RowError - all rows blank
    RowState - all rows "Unchanged"
    Table - all rows blank
    HasError - all rows have an unchecked checkbox

    Is what I am trying to do possible? If so what am I doing wrong?
    Last edited by Muddy; Dec 12th, 2007 at 01:47 PM.

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

    Re: [2008] DataGridView Control Binding with Select

    First, you need to understand how the DataSource property works. It will accept either an IList or an IListSource object. If you assign an IList object to the DataSource then it will loop through the items and display them. If you assign an IListSource object then it will call the object's GetList method to get an IList, then it will do as for an IList.

    As the name suggests, the IListSource interface exists to act as a source for an IList. The DataView class implements the IList interface, while the DataTable implements the IListSource interface. When you assign a DataTable to a DataSource property its GetList method is called, which returns its DefaultView, which is a DataView. It is the contents of that DataView that gets displayed in the grid. each item in a DataView is a DataRowView.

    When you call Select on a DataTable it returns an array of DataRow objects. There's no DataView associated with that array. In fact, the Array class itself implements IList, so when you assign that array to the DataSource it will get the DataRows themselves and display them in the grid. The DataRow class has RowError, RowState, Table and HasError properties.

    Rather than this:
    vb.net Code:
    1. DG1.DataSource = dtU.Select("CaseNumber = 0")
    you should be doing this:
    vb.net Code:
    1. dtU.DefaultView.RowFilter = "CaseNumber = 0"
    2. DG1.DataSource = dtU
    First you filetr the DefaultView, then you bind the table as normal.

    That said, you really should be using a BindingSource. You bind the DataTable to the BindingSource and the BindingSource to the grid. You would then set the Filter property of the BindingSource.
    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

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