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: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How do I paste clipboard contents bigger than GridDataBoundGrid row and column count?

 

The GridDataBoundGrid does not increment the row and column count as in the GridControl. The reason is that the GridDataBoundGrid does not have its own datastore, it has to store these in the relevant datatable. To add rows and columns in the datatable, the Model.ClipboardPaste handler must be used.

In this handler, the clipboard contents that are stored in text format of DataObject will be a single string. This has to be split with '\n' and '\t' to refer the rows and columns fashion and this count is compared with the existing row and column count. The necessary extra rows are added then and makes pasting possible.

C#

this.gridDataBoundGrid1.UseListChangedEvent = false;

DataObject data = (DataObject) Clipboard.GetDataObject();

//gets the size of the data object in rows

string s = (string)data.GetData(DataFormats.Text);

string[] rows = s.Split(new char[]{'\n'});

int numRows = rows.GetLength(0);

if(numRows > 0 && rows[numRows - 1].Length == 0)

numRows--; //removes extra empty row if present

//gets the size of the data object in Columns

string[] cols = rows[0].Split(new char[]{'\t'});

int numCols = cols.GetLength(0);

if(numCols > 0 && cols[numCols - 1].Length == 0)

numCols--; //removes extra empty column if present

int extraRowIfStartAtAddNewRow = this.gridDataBoundGrid1.Binder.IsAddNew ? 1 : 0;

while(currentCell.RowIndex + numRows + extraRowIfStartAtAddNewRow > grid.Model.RowCount)

{

DataRow dr = dt.NewRow();

dt.Rows.Add(dr);

row--;

}

while(currentCell.COlIndex + numCols > grid.Model.ColCount+1)

{

dt.Columns.Add();

col--;

}

this.gridDataBoundGrid1.Binder.ResumeBinding();

this.gridDataBoundGrid1.EndUpdate();

VB

Me.gridDataBoundGrid1.UseListChangedEvent = False

data As DataObject = CType(Clipboard.GetDataObject(), DataObject)

'gets the size of the data object in rows

s As String = CStr(data.GetData(DataFormats.Text))

rows As String() = s.Split(New Char(){ControlChars.Lf})

numRows As Integer = rows.GetLength(0)

If numRows > 0 AndAlso rows(numRows - 1).Length = 0 Then

numRows -= 1 'removes extra empty row if present

End If

'gets the size of the data object in Columns

Dim cols As String() = rows(0).Split(New Char(){ControlChars.Tab})

Dim numCols As Integer = cols.GetLength(0)

If numCols > 0 AndAlso cols(numCols - 1).Length = 0 Then

numCols -= 1 'removes extra empty column if present

End If

Dim extraRowIfStartAtAddNewRow As Integer = IIf(Me.gridDataBoundGrid1.Binder.IsAddNew, 1, 0)

Do While currentCell.RowIndex + numRows + extraRowIfStartAtAddNewRow > grid.Model.RowCount

Dim dr As DataRow = dt.NewRow()

dt.Rows.Add(dr)

row -= 1

Loop

Do While currentCell.COlIndex + numCols> grid.Model.ColCount+1

dt.Columns.Add()

col -= 1

Loop

Me.gridDataBoundGrid1.Binder.ResumeBinding()

Me.gridDataBoundGrid1.EndUpdate()

Here is a sample that illustrates this:

http://websamples.syncfusion.com/samples/KB/Grid.Windows/PastingInGDBG/main.htm

Article ID: Published Date: Last Revised Date: Platform: Control:
671 04/30/2011 07/10/2014 WinForms GridDataBoundGrid(Classic)
Did you find this information helpful?
Add Comment
You must log in to leave a comment

Please sign in to access our KB

or the page will be automatically redirected to sign-in page in 10 seconds.

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