Syncronize Datagrid scroll

Posted on 2003-03-04
Medium Priority
Last Modified: 2010-04-15
On my C# windows application form I have two datagrids,
bound to different tables. Those tables have same number
and type of columns i.e. both the grids are identical.

What I want to do is, when one of the grid is scrolled
(Horiz.) by some amount, I want the other grid also should
scroll by similar amount. If first visible column on grid
is half visible, then it should be the case for the other
grid as well.

Right now I am handling the scroll event. The logic that I am following is - when the first grid is scrolled I find its leftmost column and right most column. Then I set the current cell property of second grid to the right cell of first grid and then to the left cell of first grid.

But problem with this event is when I scroll the first grid to partial width, it doesn't scroll the second grid by similar partial amount. Only after the complete column has been scrolled on the first grid, it scrolls the column on second grid.

private void dataGrid1_Scroll(object sender, System.EventArgs e)
int leftColumn = Convert.ToInt32( dataGrid1.FirstVisibleColumn.ToString());
int rightColumn = dataGrid1.VisibleColumnCount + Convert.ToInt32( dataGrid1.FirstVisibleColumn.ToString());
dataGrid2.CurrentCell = new DataGridCell(dataGrid2.CurrentRowIndex, rightColumn);
dataGrid2.CurrentCell = new DataGridCell(dataGrid2.CurrentRowIndex, leftColumn);

Question by:ruchigup

Expert Comment

ID: 8074385
You can use two CurrencyManagers. With a simple eventhandler you can give them the same value.

Author Comment

ID: 8074768
Thanks for your comment.
I figured out one way and it works for me:-
Create a derieved class, since HorizScrollBar property and GridHScrolled method are protected.

public class MyGrid:System.Windows.Forms.DataGrid

    public int getHorizScrollBarX()
        return this.HorizScrollBar.Value;
    public void setHorizScrollBarX(int x)

        this.GridHScrolled(this, new ScrollEventArgs System.Windows.Forms.ScrollEventType.LargeIncrement, x));


Set both the datagrids to be of type MyGrid. In the scroll event just write.

private void dataGrid1_Scroll(object sender, System.EventArgs e)
    int x = dataGrid1.getHorizScrollBarX();


Accepted Solution

modulo earned 0 total points
ID: 11805718
PAQed, with points refunded (50)

Community Support Moderator

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…

621 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