Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to link two DataGridViews

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

860 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