none
Dataset/databable binding to datagridview question RRS feed

  • Question

  • Hello,

    I am working on a new c# windows forms application. 

    Through the designer, I created a dataset with a single data table. 

    On the form I added a datagridview to display the data in the datatable.  In the dgv's DataSource property, I pointed to the dataset.  In the dgv's DataMember property I pointed to the DataTable.  This last action caused two additional objects to be created in the designer, a DataSetName object and a DataBindingSource object.  Now the dgv points to the bindingsource object.  The grid now shows all of my columns, and I can reorder them and change various properties on them.

    All is okay so far. 

    When I execute the program and the datatable is filled, nothing show in the dgv.  My belief was that the designer had created all of the necessary "hooks" to display the data, but apparently not.

    I added this code to the program:

     

    BindingSource myBS = new BindingSource();
    myBS.DataSource = dsDHCP.DTDHCP;
    dataGridView1.DataSource = myBS;

    and now the grid works perfectly.

    Can someone help me understand what I am missing about the binding of the table to the dgv, and why the additional code is necessary to get the dgv to display the data?

    Thanks for any and all help.

    Bryan Hunt

    Friday, June 12, 2009 6:05 PM

Answers

  • Chris,

    I never did get that to work.  I tried every combination/permutation that I could.

    Basically, following your three steps above allowed me to see and manipulate the columns in the designer. 

    But, after adding data to the table, nothing would ever appear without executing this method:

         myBS.DataSource = dsDHCP.DTDHCP;

    before returning control to the form.

    Which is okay, certainly no strain to execute that command after loading the datatable.  Just doesn't seem like I should have to...

    Thanks for your help on this.

    Bryan Hunt

    Friday, June 19, 2009 5:31 PM

All replies

  • Hey Bryan,

    Select the databindingSource, it should have a DataSource. I would set the datasource of this property to the created DataSet on the form and put the DataMember being the table name in the databindingSource's DataMember. You should be able to do this all through the designer instead of having to write code. It seems to me that the DataSource property isn't set on the databindingsource. The databinding source is just an intermediate layer.

    Thanks
    Chris Robinson
    Program Manager - DataSet
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, June 12, 2009 9:22 PM
  • Chris,

    Not sure exactly where you are in these selections.  Perhaps you could guide me through.

    BTW, using VS.2008.

    So I am looking at Properties of my dgv.  I see a DataSource property, but no databindingsource property?  If I set the DataSource property of the dgv to the DataSet, the two additional objects are added.  I then go to the bindingsource object and add the datatable as its datamember.  I then run, and still get nothing in the grid.

    Do I have the steps right?

    Thanks.

    Bryan Hunt
    Friday, June 12, 2009 9:48 PM
  • Ahh, so I used the wrong terminalogy. The DataSource of the DataGridView should be set to the bindingSource object. Check what is set for the datasource of the bindingsource. When I was writing up I should have said bindingsource. It is the intermediate layer between the table and the grid.

    So first do this
    1) Verify the DataSource of the DataGridView is set to the bindingsource
    2)Verify the DataSource of the bindingsource is set to the DataTable (or DataSet)
    3)If the DataSet is set as the datasource of the bindingsource then set the name of the table as the DataMember. other wise it should all work

    Thanks
    Chris

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Saturday, June 13, 2009 12:34 AM
  • Chris,

    I never did get that to work.  I tried every combination/permutation that I could.

    Basically, following your three steps above allowed me to see and manipulate the columns in the designer. 

    But, after adding data to the table, nothing would ever appear without executing this method:

         myBS.DataSource = dsDHCP.DTDHCP;

    before returning control to the form.

    Which is okay, certainly no strain to execute that command after loading the datatable.  Just doesn't seem like I should have to...

    Thanks for your help on this.

    Bryan Hunt

    Friday, June 19, 2009 5:31 PM
  • Hey Bryan,

    Thanks for letting me know. I think that somewhere in the code there has to be something that calls Fill on the adapters and fills the DataSet, Perhaps this was what was needed. If you are working on VS2008 there is also a TableAdapterManager that is generated. You can call fill on it and it will fill all the tables. You can do this on FormLoad, this would ensure the data would be in the DataSet.

    It seems that your line of code:

    myBS.DataSource = dsDHCP.DTDHCP;

    points to a particular table.

    If its working its working,

    Chris


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, June 19, 2009 9:15 PM