Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Synchronize scroll on DataGrid

Status
Not open for further replies.

crystalruchs

Programmer
Sep 6, 2002
11
US
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.Refresh();
Application.DoEvents();
dataGrid2.CurrentCell = new DataGridCell(dataGrid2.CurrentRowIndex, leftColumn);
}



 
The scroll object has a property named value, the following text is from the help:

Value (inherited from ScrollBar) Gets or sets a numeric value that represents the current position of the scroll box on the scroll bar control.

ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemwindowsformshscrollbarmemberstopic.htm

You can use this property to set and get the value for the scroll...

Larsson
 
You are right that value property makes the scroll bar to change its position, but the problem is it doesn't scroll the grid also.
Following solution 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.HorizScrollBar.Value = 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();
dataGrid2.setHorizScrollBarX(x);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top