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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I scroll a datagrid from the program 1

Status
Not open for further replies.

mondi

Programmer
Sep 10, 2003
169
AL
Hello!
I have in a windows form, two datagrids. The first datagrid has one row and is positioned on the top of the second datagrid and serves as rowheader for the second one. The scroll bar of the first is not visible and the grid is supposed to scroll when the user scrolls the second datagrid. But I just don't know how to raise the scroll event for the first datagrid in that time.
I overrided the GridHScrolled() method in my class (I have made a DataGridCl that inherits from the DataGrid class), because I thought it might help me, but I didn't find a way how to do it.
Any help would be very appreciated!
Thanks

Country of eagles
 
I used the following code for vertically scrolling two grids, but it's the same principle.

In your derived grids, add the following code:
Code:
public delegate void GridVScrolledEventHandler(object sender, ScrollEventArgs se);

public event GridVScrolledEventHandler GridVerticallyScrolled;

protected override void GridVScrolled(object sender, ScrollEventArgs se)
{
	base.GridVScrolled (sender, se);

	if (GridVerticallyScrolled != null) GridVerticallyScrolled(this, se);
}

public void ScrollGridVertically(object sender, ScrollEventArgs se)
{
	this.GridVScrolled(this, se);
}
In your form, add the following:
Code:
dgChild.GridVerticallyScrolled += new DataGridCl.GridVScrolledEventHandler(dgChild_GridVerticallyScrolled);

private void dgChild_GridVerticallyScrolled(object sender, ScrollEventArgs se)
{
	dgParent.ScrollGridVertically(sender, se);
}
Basically, whenever the child grid scrolls the GridVerticallyScrolled event is raised, causing the parent grid to also scroll by the same amount. Just don't subscribe both grid's to each others GridVerticallyScrolled events or they will fire each other in a continuous loop.
 
Thanks a lot SHelton. This code with the modifications for the horizontal scrollbar worked fine. You deserve a big star :)

Country of eagles
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top