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

Copy and Paste including cell style

Hi, I'd like to include the cell style when I copy and paste data within my virtual grid. I've tried setting the ClipboardFlags and calling CopyCellsToClipboard, but I never seem to get the style information in my SaveCellInfo call, and the data in the clipboard only appears to be text. What do I need to do? Thanks, Sue

6 Replies

AD Administrator Syncfusion Team September 16, 2003 04:17 PM

The grid by default will copy styles. Here is a little sample. If you copy any of the red cells in column 1, and paste them somewhere else, the color is known in SaveCellInfo (though it is not saved by the code in the sample). What is different about what you are trying?


SH Sue Harris September 16, 2003 05:14 PM

> The grid by default will copy styles. Here is a little sample. If you copy any of the red cells in column 1, and paste them somewhere else, the color is known in SaveCellInfo (though it is not saved by the code in the sample). > > What is different about what you are trying? Excellent, your sample let me see whats different. I was trying to copy a single cell (my activate behaviour is double click, so it isn't actually editing), and that seems to use a different copy mechanism (the renderer copy method I presume). Can I modify the copy technique to use the cell copy rather than the render copy, and so copy the style format? Thanks, Sue


AD Administrator Syncfusion Team September 16, 2003 05:45 PM

You can try catching this event and doing the copy yourself in this one-cell case.
private void gridControl1_ClipboardCopy(object sender, GridCutPasteEventArgs e)
{
	if(e.RangeList.Count == 0)
	{
		GridRangeInfoList list = new GridRangeInfoList();
		list.Add(this.gridControl1.CurrentCell.RangeInfo);
		this.gridControl1.CutPaste.CopyCellsToClipboard(list, true);
		e.Handled = true;
		e.Result = true;
	}
}


SH Sue Harris September 16, 2003 06:11 PM

Thanks Clay, That did the trick. My only disappointment was that there wasn't an OnClipboardCopy method in my inheriting control that I could override (I did have to hook the event, rather than override). Thanks, Sue


SH Sue Harris September 17, 2003 09:28 PM

Clay, The copy doesn't appear to be getting the row/column styles. Is this possible? I added the following to the QueryCellInfo method and tried copying and pasting from cells in the second column, and got color [Window] instead of blue. else if ((e.ColIndex == 2) && (e.RowIndex == -1 )) { e.Style.BackColor = Color.Blue; } Thanks, Sue


AD Administrator Syncfusion Team September 18, 2003 03:22 AM

The style is not being composed in the copy code, so you only get the cell attributes. If you look at the source code, there is a //todo section marked to implement a composing styles option but it has not been implemented yet. So for now, if you want the row/col styles to be copied along with the cell style for a cell, you will have to do th process yourself. Below is some code that worked for me.
private void gridControl1_ClipboardCopy(object sender, GridCutPasteEventArgs e)
{
	if(e.RangeList.Count == 0)
	{
		//get a copy of existing cell style
		GridCurrentCell cc = this.gridControl1.CurrentCell;
		GridStyleInfo style = new GridStyleInfo(this.gridControl1[cc.RowIndex, cc.ColIndex]);

		//apply QueryCellInfo row/col attributes
		GridStyleInfo style1 = new GridStyleInfo();
		//row  changes
		this.gridControl1.Model.GetCellInfo(cc.RowIndex, -1, style1);
		style.ModifyStyle(style1, Syncfusion.Styles.StyleModifyType.ApplyNew);
		//col  changes
		this.gridControl1.Model.GetCellInfo(-1, cc.ColIndex, style1);
		style.ModifyStyle(style1, Syncfusion.Styles.StyleModifyType.ApplyNew);
				
		//save the data to the clipboard
		GridData data = new GridData();
		data.RowCount = 1;
		data.ColCount = 1;
		data[0,0] = style.Store;
		Clipboard.SetDataObject(data);
	}
}


Loader.
Live Chat Icon For mobile
Up arrow icon