We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How to synchronize two grids appearance at run-time

Say I have two DataBoundGrids in a User Control. How to sync the two grids? For example, when I resize a Column1 in Grid1, I also want Column1 in Grid2 to be resized simultaneously (when dragging the Column1 in either grids). And, when there''s a scroll bar in Grid1, I want Grid2 to have a scroll bar too (scrolling should occur in both grids simultaneously). Is this possible? which events triggers this? do u have sample code for this situation? NB: both grids will have the same amount of columns with same width, they might contain different values though. thanks.

1 Reply

AD Administrator Syncfusion Team March 30, 2005 08:27 AM UTC

Yes, this is possible. For each of the actions you want to synchronize, you will have to catch an appropriate event on each grid. In your event handlers, you would have to use code to explicitly make the particular action occur on the grid that did not generate the event. If you use teh same handler code for both grids, you would need to make sure you avoid recursive calls (like changing colwidth in first grid triggerring colwidth change in second grid triggerring colwidth change in first grid and so on. To handle the two actions you mentioned, you can handle these events. this.gridDataBoundGrid1.TopRowChanged += new GridRowColIndexChangedEventHandler(grid_TopRowChanged); this.gridDataBoundGrid2.TopRowChanged += new GridRowColIndexChangedEventHandler(grid_TopRowChanged); this.gridDataBoundGrid1.Model.ColWidthsChanged += new GridRowColSizeChangedEventHandler(Model_ColWidthsChanged); this.gridDataBoundGrid2.Model.ColWidthsChanged += new GridRowColSizeChangedEventHandler(Model_ColWidthsChanged);
private bool inTopRowChanged = false; 
private void grid_TopRowChanged(object sender, GridRowColIndexChangedEventArgs e)
{
	if(!inTopRowChanged)
	{
		inTopRowChanged = true;
		GridDataBoundGrid master = sender as GridDataBoundGrid;
		GridDataBoundGrid slave;
		if(master == this.gridDataBoundGrid1)
			slave = this.gridDataBoundGrid2;
		else
			slave = this.gridDataBoundGrid1;

		slave.TopRowIndex = master.TopRowIndex;
		inTopRowChanged = false;
	}
}

private bool inColWidthsChanged = false;
private void Model_ColWidthsChanged(object sender, GridRowColSizeChangedEventArgs e)
{
	if(!inColWidthsChanged)
	{
		inColWidthsChanged = true;
		GridModel master = sender as GridModel;
		GridModel slave;
		if(master == this.gridDataBoundGrid1.Model)
			slave = this.gridDataBoundGrid2.Model;
		else
			slave = this.gridDataBoundGrid1.Model;

		slave.BeginUpdate();
		for(int col = e.From; col <= e.To; ++col)
		{
			slave.ColWidths[col] = master.ColWidths[col];
		}
		slave.EndUpdate();
		slave.Refresh();
		inColWidthsChanged = false;
	}
}

Loader.
Live Chat Icon For mobile
Up arrow icon