Results 1 to 5 of 5

Thread: [RESOLVED] Binding a DataGridView with 2 tables?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Resolved [RESOLVED] Binding a DataGridView with 2 tables?

    This is a 2-part question.

    Part 1:
    I have a datagridview who's datasource is a BindingSource. The problem that I need to resolve is that the DGV has 6 columns, 5 of which come from the same database table, while the other column comes from another database table. Is there anyway of configuring the BindingSource that will make it contain all 6 columns?

    Part 2:
    The same grid in Part 1 works in the way that when a user selects a row, textboxes and comboboxes are populated with the DGV Row of data in order for the data to be modified. When the user makes any changes, this modified data needs to update the DGV row or BindingSource. How do I associate the changed values back to the BindingSource or the DGV?

    Thanks,
    Last edited by blakemckenna; Mar 8th, 2010 at 06:07 PM.
    Blake

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

    Re: Binding a DataGridView with 2 tables?

    1. You can only bind to a single object. Your DataGridView binds to a single BindingSource and your BindingSource binds to a single DataTable. If you want that DataTable to contain data from multiple database tables then that's what your query is for. Simply write the appropriate join in your SQL code.

    2. You simply bind your other controls to the same BindingSource. Any changes in the TextBoxes, etc, updates the data source and that updates the grid. It's all automatic, which is the whole point of data-binding.
    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
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Binding a DataGridView with 2 tables?

    jmc,

    For part 1, I actually tried that before I read your response and that worked. As far as part 2, that worked really well. For some reason, I like to write the SQL statements and see the code that produces the output. Do you know of a good reference book about DataBinding?

    Thanks,
    Blake

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

    Re: Binding a DataGridView with 2 tables?

    Quote Originally Posted by blakemckenna View Post
    Do you know of a good reference book about DataBinding?
    I'm sure most books that cover WinForms cover it to some degree but I learned from MSDN, other web resources and by doing.
    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
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Binding a DataGridView with 2 tables?

    thx...
    Blake

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