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

pasting into cell from text file

Hi, I have a text file containing multiple lines separated by ''s. When a user selects all the text in the file and pastes it (using ctrl+v)into my grid, it overwrites more than just the first cell, it puts one line in each column and then moves onto the next record and overwrites cells in that record. I would instead like to be able to remove the ''s and paste all of the text into only the first cell. How do I do this? Thanks! Laurie

4 Replies

AD Administrator Syncfusion Team December 16, 2003 05:12 AM UTC

You can use the ClipboardPaste event (Model.ClipboardPaste in a GridDataBoundGrid) to do this. Below is a code snippet suggesting how you might go about it.
private void gridControl1_ClipboardPaste(object sender, GridCutPasteEventArgs e)
{
	IDataObject data = Clipboard.GetDataObject();
	if(data != null && data.GetDataPresent(typeof(string)))
	{
		string s = (string)data.GetData(typeof(string));
		GridRangeInfoList rangeList;
		if(this.gridControl1.Selections.GetSelectedRanges(out rangeList, true))
		{
			if(rangeList.ActiveRange.IsCells)
			{
				int row = rangeList.ActiveRange.Top;
				int col = rangeList.ActiveRange.Left;
				if(this.gridControl1.CurrentCell.IsEditing)
					this.gridControl1.CurrentCell.CancelEdit();
				s = s.Replace("", "");
				s = s.Replace("\r", "");
				this.gridControl1[row, col].Text = s;
				e.Handled = true;
				e.Result = true;
			}
		}
	}
}


LS Laurie Spencer December 16, 2003 05:13 PM UTC

Thanks! That got me a long ways towards a solution. However, it seems to be bypassing some of the edit control functionality. It replaces all content in the cell rather than replacing selected text or pasting at an insertion point. Here''s what I have: if (CurrentCell.Renderer.ValidateString(text)) { this[row, col].ApplyFormattedText(text, GridCellBaseTextInfo.PasteText); } Is there something similar to ApplyFormattedText that will respect current text selection and insertion point? Thanks! Laurie


AD Administrator Syncfusion Team December 16, 2003 07:56 PM UTC

This is the code that forces the text to be replaced in an active cell. if(this.gridControl1.CurrentCell.IsEditing) this.gridControl1.CurrentCell.CancelEdit(); If you want to paste the text into an editing currentcell, the get the GridTextBoxControl from the cellrenderer, and set the text there.
if(rangeList.ActiveRange.IsCells)
{
	int row = rangeList.ActiveRange.Top;
	int col = rangeList.ActiveRange.Left;
	s = s.Replace("", "");
	s = s.Replace("\r", "");
	if(this.gridControl1.CurrentCell.IsEditing)
	{
		GridTextBoxControl tb = this.gridControl1.CurrentCell.Renderer.Control 
		                     as GridTextBoxControl;
		if(tb != null)
			tb.Text = tb.Text.Substring(0, tb.SelectionStart) 
				+ s 
				+ tb.Text.Substring(0, tb.SelectionStart + tb.SelectionLength );
	}
	else
	{
		this.gridControl1[row, col].Text = s;
	}
	e.Handled = true;
	e.Result = true;
}


LS Laurie Spencer December 17, 2003 01:46 AM UTC

That did it! Thanks! -Laurie

Loader.
Live Chat Icon For mobile
Up arrow icon