Solved

How to link two DataGridViews

Posted on 2007-04-05
2
756 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

948 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now