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
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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

762 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