CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2005

    Linking two datagridviews (keep on same row)

    I have two datagridviews, one represents existing data which is read only, the other shows another table, with the same structure, but allows the user to change the values. Starting out, the grids will be exactly the same, and the first grid only acts as a reference to what the data was like before the uesr changes it, allowing the user to change values on the second grid, while referencing the first grid if needed.

    Now, the grid is more vertical than horizontal, so they will have a vertical scroll bar on both grids. How can I link the grids, so that if the user moves to the 5th row of one of the grids, the other grid moves down to that row? Also, when the user scrolls one grid, I want the other grid to scroll exactly the same. Get my drift?



  2. #2
    Join Date
    May 2005

    Re: Linking two datagridviews (keep on same row)

    Wow, I found a solution right after I decided to post this question...

    in the scroll event of the grids, I set the other grid's .firstDisplayedScrollingRowIndex = to the value of the grid being scrolling's .firstDisplayedScrollingRowIndex

    It works like a charm

  3. #3
    Join Date
    Oct 2003
    .NET2.0 / VS2005 Developer

    Re: Linking two datagridviews (keep on same row)

    You could also consider using a DataRelation so that grid 2 only shows the relation of the currently highlighted row in grid 1
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  4. #4
    Join Date
    May 2005

    Re: Linking two datagridviews (keep on same row)

    The two grids always have the same rows, it is just they most likely will have a scroll bar attached to them, so that if there are 50 rows, and you want to scroll down to the 35th row, the other grid will scroll along with it, so basically each row on both grids will always be int he same position.

    I am now having the problem of selecting the same row when the user clicks a row on one of the grids. Lets say you select the 5th row on the left grid, the 5th row on the right grid should select.

    I used the selectionChanged event, but am getting index out of range because it seems like this event fires when both grids aren't fully populated. I am trying to write a workaround, but basically I have this now, which isn't working..

    private sub grid1_selectionChanged
    grid2.Rows(grid1.SelectedRows(0).Index).Selected = True
    end sub
    How can I not allow this from running, until basically all of the rows are populated?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.