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 multiple cells

Microsoft Excel allows me to select multiple cells and paste a value into the selected cells. How is that possible with Syncfusion Grid Control?

7 Replies

JH Jamie Hill July 17, 2003 07:51 PM UTC

I too had the same problem. I came up with this solution, but there might be something else better. This will handle the ClipboardPaste event and call another function which returns a boolean value for whether or not the paste was handled. There is alot of conditional checking to see if the paste is a single row and/or single column. This allows for a big blob of data to still be pasted into the grid, while handling the case of pasting one piece of data into multiple cells. JH Private Sub DataGrid1_ClipboardPaste(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridCutPasteEventArgs) Handles DataGrid1.ClipboardPaste Dim blnHandled As Boolean blnHandled = ClipboardPaste(DataGrid1) e.Handled = blnHandled End Sub Private Function ClipboardPaste(ByVal dg As Grid.GridControl) As Boolean Try Cursor = Cursors.WaitCursor Dim intRow, intCol As Integer Dim objRow As Grid.GridRangeInfo Dim objRange As Grid.GridRangeInfo = dg.Selections.Ranges.ActiveRange ' Declares an IDataObject to hold the data returned from the clipboard. ' Retrieves the data from the clipboard. Dim iData As IDataObject = Clipboard.GetDataObject() Dim strText() As String 'If there is more than one row, return false so that the paste can proceed. strText = Split(CType(iData.GetData(DataFormats.Text), String), vbNewLine) If strText.GetUpperBound(0) > 1 Then Return False Exit Function End If 'If there is more than one column, return strText = Split(CType(iData.GetData(DataFormats.Text), String), vbTab) If strText.GetUpperBound(0) > 0 Then Return False Exit Function End If 'If the paste is a single cell, return false so that the paste can proceed. If objRange.Top = objRange.Bottom And objRange.Left = objRange.Right Then dg(dg.CurrentCell.RowIndex, dg.CurrentCell.ColIndex).Text = "" Return False End If 'Loop through the selected rows and paste the value into each cell. For intRow = objRange.Top To objRange.Bottom Try Cursor = Cursors.WaitCursor 'Paste into each cell for the row. For intCol = objRange.Left To objRange.Right 'MessageBox.Show(intRow.ToString & " " & intCol.ToString) ' Determines whether the data is in a format you can use. If iData.GetDataPresent(DataFormats.Text) Then 'Put the clipboard value into the cell. dg(intRow, intCol).Text = CType(iData.GetData(DataFormats.Text), String) End If Next Catch ex As System.Exception MessageBox.Show(ex.ToString) Finally Cursor = Cursors.Default End Try Next Return True Catch ex As System.Exception HandleError(ex) Finally Cursor = Cursors.Default End Try End Function


PB Philip Bishop August 10, 2004 04:55 PM UTC

I was wondering if you could help me make this do one other thing its lacking on. This codes seems in early testing to work fine but if the user selects the column header cell to select the whole column then it wont paste right. If u select one cell and copy it and then select a column header and paste it, it will only paste in the first cell of the selected column. I''m using a non data bound grid in vs 2003. the cell type is currency and the activate current celle behavior is set to double click on cell. Any ideas on this???


AD Administrator Syncfusion Team August 10, 2004 07:13 PM UTC

After getting objRange, expand it so that it reflects a range of cells. objRange = objRange.Expand(1, 1, dg.RowCount, dg.ColCount)


PB Philip Bishop August 11, 2004 09:51 AM UTC

Ok so i have one last question I think on all of this. Does this process of copying one to many using that clipboardpaste function now make it so it wont go to the pastecelltext event? It appears as if it doesnt go there at all now. Is it as simple as adding the validating code to the clipboardpaste function??


AD Administrator Syncfusion Team August 11, 2004 10:03 AM UTC

Setting e.Handled = true in ClipboardPaste tells the grid that you handled the pasting, and it should not execute it''s default paste code (which raises the PasteCellText validation event). So, in this case, if you want to validate things, then you would have to initiate the validation in some manner. You could either just do it directly in your ClipboardPaste method, or you could raise an event there that listeners could subscribe to, and allow them to validate the paste.


MJ MJ March 20, 2020 09:44 AM UTC

Pasting into multiple cells.

Microsoft Excel allows me to select multiple cells and paste a value into the selected cells?

Please reply..


AR Arulpriya Ramalingam Syncfusion Team March 23, 2020 06:34 AM UTC

Hi MJ, 
 
Thank you for the update. 
 
The GridControl have the built-in support to copy and paste multiple cells content. The ActivateCurrentCellBehavior can be set to DblClickOnCell to avail this behavior since, the current cell content will only be copied when the CurrentCell is in editing mode. Please make use of the below code and sample. 
 
Example code 
 
//To activate the current cell on double click and copy the selected cells content. 
gridControl1.ActivateCurrentCellBehavior = GridCellActivateAction.DblClickOnCell; 
 
 
Please get back to us, if you have any other queries. 
 
Regards, 
Arulpriya 


Loader.
Up arrow icon