Solved

How to link two DataGridViews

Posted on 2007-04-05
2
761 Views
Last Modified: 2013-12-25
On a Windows Form I have two DataGridViews, one for each of two related tables.  They are related on a common Key_ID field.  I want them to always stay linked on that Key_ID so that the selected rows stay in sync and if Key_ID is selected in on DataGridView the row with the same Key_ID is selected in the other DataGridView.

I'm currently using a ComboBox bound as follows to link them:
Data Source:  BindingSource1
Display Member:  Some Text
Value Member: Table1.Key_ID
Selected Value: Table2.Key_ID

The problem with this is that when I scroll down through the rows the textbox I have capturing the information I need is a row behind.  It displays the value from the row I have just left not the current row.  I have a SQL query run on the ComboBox.SelectedIndexChanged event.

I'd like to do away with the combobox entirely and just link the two DataGridView directly.  Any ideas?  
I'm using VB.NET in VS2005, .Net 2.0

Thanks.
David
0
Comment
Question by:megnin
2 Comments
 
LVL 11

Accepted Solution

by:
Babycorn-Starfish earned 500 total points
ID: 18940225
Hi, assuming you are using BindingSource objects to link the controls to the dataset you can capture CurrentChanged event, once this fires you can use code of this form

String key = ((DataRowView)(FirstBindingSource.Current))["KeyColumn"].ToString();
SecondBindingSource.Position = SecondBindingSource.Find("SecondKeyColumn", key);

this way when the position changes in the first binding source it will select the equivalent row that has a column with the key in.
0
 
LVL 1

Author Comment

by:megnin
ID: 18958849
Wonderful!  Exactly with I was looking for.

Thank you very much!

David
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to prevent website from publishing with errors 9 46
Code works but it's slow 28 70
No Data for DropDown List 2 24
C# LINQ ForEach() question 6 27
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question