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. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

pasting into cell from text file

Thread ID:

Created:

Updated:

Platform:

Replies:

9389 Dec 16,2003 03:56 AM UTC Dec 17,2003 01:46 AM UTC WinForms 4
loading
Tags: GridControl
Laurie Spencer
Asked On December 16, 2003 03:56 AM UTC

Hi, I have a text file containing multiple lines separated by \n''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 \n''s and paste all of the text into only the first cell. How do I do this? Thanks! Laurie

Administrator [Syncfusion]
Replied On 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("\n", "");
				s = s.Replace("\r", "");
				this.gridControl1[row, col].Text = s;
				e.Handled = true;
				e.Result = true;
			}
		}
	}
}

Laurie Spencer
Replied On 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

Administrator [Syncfusion]
Replied On 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("\n", "");
	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;
}

Laurie Spencer
Replied On December 17, 2003 01:46 AM UTC

That did it! Thanks! -Laurie

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;