none
Using a bridge table for a M:N relationship - How to set the Bindingsource? RRS feed

  • Question

  • I'm using .NET 2.0, Visual Studio 2005 and coding a Windows Forms application in C#.  I have the classic example of a many-to-many table between emps and depts.  So I built a bridge table with the id of each of these tables.  When it comes to the bindingsource, using the foreign key data member just shows me those ids in the datagrid.

     

    I AM NOT advocating that anyone code this way, but the quickest (and dirtiest) way to see how this is working is to build an emp table, a dept table and an empdept table.  Dragging the emp details onto the form and the dept grid onto a tabPage will either show all the depts or just the ids (if the foreign key is chosen as the data member).  I know it has to do with the relationships, but I don't know how to resolve it...that is, did I set up my bridge table incorrectly, or did I set up my bindingsource incorrectly?

     

    Thanks a million!!

     

    Sunday, September 9, 2007 11:42 PM

All replies

  • I guess I should clarify to say that I am using DataSets and need the user to be able to just lookup an emp and add/delete a dept for that emp.  I realize that I could write a view (and I did), but I don't think this is the place to use the view...unless I can make it updateable?????  Thanks again.

     

     

    OK.  I think I should have logged this in Windows Forms Data Binding group.  I don't know how to change that, so please feel free, Moderator, to remove these two posts.  I will repost in WinForms.  Thanks for your patience with newbies....

     

    Monday, September 10, 2007 12:10 AM
  • I don't think this is really a data-binding question.  The problem here is not so much "How do I bind controls to this data?" as it is "What data should I be working with in the first place?

     

    It depends on your use cases.  If you want to give the user a way to select an employee and add/delete the employee/department association, it's pretty straightforward.  You need three BindingSources:  one for employees, one for departments, and one for associations.  You use the employee BindingSource as the source of a dropdown list whose ItemSelected event sets the Filter on the association BindingSource.  The association BindingSource is the data source for a DataGridView that displays departments in ComboBox columns; the department BindingSource is the data source for that ComboBox column's List.

     

    This is pretty easy, as far as this stuff goes.  It gets unwieldy once dropdown lists aren't the right solution for selecting employees (or, worse, departments, as you have a lot less latitude in what you can do in a DataGridViewCell).  And high volumes of data will complicate this a lot.  But for most use cases, it's not that hard.

    Monday, September 10, 2007 5:05 AM