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

Copying a Grid

Hi, I''ve got a grid which is being populated using the virtual QueryCellInfo, QueryColCount etc methods. I''d like to take a copy of the grid into another non-virtual grid. What would be the best way to do this? Thanks, Sue

1 Reply

AD Administrator Syncfusion Team January 19, 2005 06:35 AM UTC

There are basically 2 types of information you will need to copy. One is the actual data, and the other are things stored in the GridModel like ColWidths, etc. To copy the GridModel, you can serialize it to a memorystream and then create a new instance from the serialized data. System.IO.MemoryStream s = new System.IO.MemoryStream(); oldGrid.SaveBinary(s); GridControl newGrid = new GridControl(GridModel.LoadBinary(s)); s.Close(); The quickest way to handle teh actual data is to do it directly through your external datasource, and not try to use the oldGrid. The reason is that if you go through the oldGrid and use an indexer on this grid to retrieve the data, events (such as QueryCellInfo that actually gets the data) will be raised. If you access your datasource directly, then these events will be avoided which normally makes things much faster. To put the data into your new grid, if your datasource is something recognized by the grid.PopulateValues method, you should use that method. If not, you can loop through your datasource and call grid.SetCellInfo to set the values into the grid. Make sure you use the overload that has dontRaiseSaveCellInfo and pass this as true. Or, you can just directly set the values into the GridData object yourself.
newGrid.RowCount = rowsInDataSource;
newGrid.ColCount = colsInDataSource;
for(int row = 1; row <= rowsInDataSource; row++)
{
	for(int col = 1; col <= colsInDataSource; col++)
	{
		object o = GetValueFromYourDataSource(row, col);
		if(o != null)
		{
			GridStyleInfo style = new GridStyleInfo();
			style.CellValue = o;
			newGrid.Data[row, col] = style.store;
		}			
	}
}
newGrid.Refresh();

Loader.
Live Chat Icon For mobile
Up arrow icon