Syncronize Datagrid scroll
Posted on 2003-03-04
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);