Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How to have one data bound control determine the contents of another?
 

How to have one data bound control determine the contents of another?

I'm making a VS2k8 C# forms app that has a listcontrol and two datagrids as the primary UI. The listcontrol is going to be populated with the contents of a table and I'd like the program to respond to a change in selection of this listcontrol by populating the first datagrid with items from a database that correspond to the selected item in the list control. Likewise, when the user selects a row in the first datagrid, the second datagrid is to be populated with appropriate data from the database.

To put it simply, there are three tables in the database. The first table has an ID and a string. The second table has an ID, a foreign key that is the first table's ID and a few other things. The third table has an ID, a foreign key that comes from the second table's ID and a few other things. Standard sort of thing.

The obvious thing to do is to add an event that responds to a user selection of the listbox. However, then what? How can I get from a selected index in the listcontrol to the corresponding item's ID? Once I have that ID, how do I modify the first datagrid's population command so that it only displays those items from the second table that have the current listcontrol selected item's ID? Likewise, when the first datagrid gets a selected item, how can this event be used to trigger the population of the second datagrid's contents with those items from the third table that have the ID of the first datagrid's selected item?

Richard.Haggard  Monday, August 31, 2009 3:37 PM
Hello Richard,

Thanks for your feedback.

>Does the suggested method allow multiple selection?

Based on my understanding, it is not supported. Actually, it uses the current row(return from CurrencyManager. Current ) to filter the detailed DataGridView.

More info
http://msdn.microsoft.com/en-us/library/system.windows.forms.currencymanager.current.aspx

>Is it possible to simply generate select strings for the controls?

Sure, most list control has a property that returns all the selected item, we can use this to get all the select IDs and then use these IDs to select the child rows from our database.

Thanks,
Rong-Chun Zhang
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Rong-Chun Zhang  Wednesday, September 02, 2009 8:19 AM
Hello Richard,

Thanks for your post on MSDN forum.

Data binding mechanism supports binding to a related datasource, that means when selecting a item in the mater table, the detail table will be filtered automatically. Please take a look at the following article, it demonstrates how to use two DataGridView controls to show related datatables.

Walkthrough: Creating a Master/Detail Form Using Two Windows Forms DataGridView Controls
http://msdn.microsoft.com/en-us/library/y8c0cxey.aspx

Although, the article only shows how to bind to two datatables, the logic is the same to bind to three datatables.
http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/4b8e7ec1-f175-4a31-96b0-12e5d9335ad4

Thanks,
Rong-Chun Zhang
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Rong-Chun Zhang  Tuesday, September 01, 2009 7:29 AM
Thanks. I took a look at the suggested methodology but had a little trouble squashing it into something that the IDE wanted to allow. The suggested design is easy enough to understand. A single DataSet is filled with the entire contents of all tables to be displayed, a DataRelation object is created, used to establish a relationship between two tables within the database and then the UI controls are attached to the appropriate table within the DataSet. The silly IDE, of course, has its own ideas of what to do. It defaults to creating a rather Rube Goldbergian series of objects that eventually get back to the original tables in the database, each control getting its own channel.

As an aside, does the suggested method allow multiple selection?

While I can certainly wade through things and make it work in the suggested fashion, is it possible to simply generate select strings for the controls?
Richard.Haggard  Tuesday, September 01, 2009 8:26 PM
Hello Richard,

Thanks for your feedback.

>Does the suggested method allow multiple selection?

Based on my understanding, it is not supported. Actually, it uses the current row(return from CurrencyManager. Current ) to filter the detailed DataGridView.

More info
http://msdn.microsoft.com/en-us/library/system.windows.forms.currencymanager.current.aspx

>Is it possible to simply generate select strings for the controls?

Sure, most list control has a property that returns all the selected item, we can use this to get all the select IDs and then use these IDs to select the child rows from our database.

Thanks,
Rong-Chun Zhang
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Rong-Chun Zhang  Wednesday, September 02, 2009 8:19 AM

Have you got any progress on this issue? If there is anything else we can help, welcome to post here.

If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.

Thanks,
Rong-Chun Zhang


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Rong-Chun Zhang  Friday, September 04, 2009 9:31 AM

You can use google to search for other answers

Custom Search

More Threads

• Custom DataGridViewCell
• Make selected rows in a datagridview read only
• BUG in data grid control
• How to get updated rows when grid binded to a binding list collection?
• The rowstate for the selected row in a child datagrid remains unchanged in a hierarchical scenario
• Disable Rows In dataGridView ?
• user profiles in gridview
• DataGridViewButton
• Stoopid Newbie question: .AddNew
• Data Grid