Solved

How to link two DataGridViews

Posted on 2007-04-05
2
749 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
Comment Utility
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
Comment Utility
Wonderful!  Exactly with I was looking for.

Thank you very much!

David
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

744 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

17 Experts available now in Live!

Get 1:1 Help Now