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