Hi,
I want to know if this is possible to copy the data or records from a cell & then paste it somewhere else?
What property should be used or I need to code something for this to make it possible?
Printable View
Hi,
I want to know if this is possible to copy the data or records from a cell & then paste it somewhere else?
What property should be used or I need to code something for this to make it possible?
You have to learn this for what you are asking!
http://www.codeproject.com/KB/databa...opyHelper.aspx
and
http://www.codeproject.com/KB/webfor...rtToExcel.aspx
I hope this helps you!
Copy datagridview selection to clipboard
Code:Private Sub ctxCopyAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ctxCopyAll.Click
CopyAllRowsToClipboard()
End Sub
Private Sub CopyAllRowsToClipboard()
Dim intRow As Int32 = 0
Dim drRow As DataGridViewRow = Nothing
Dim strClipboard As String = String.Empty
For Each drRow In grdBudgetData.Rows
drRow.Selected = True
grdBudgetData.SelectAll()
If intRow = 0 Then
grdBudgetData.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText
Else
grdBudgetData.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText
End If
If Me.grdBudgetData.GetCellCount(DataGridViewElementStates.Selected) > 0 Then
Try
‘ Add the selection to the clipboard.
Clipboard.SetDataObject(Me.grdBudgetData.GetClipboardContent())
‘ Replace the text box contents with the clipboard text.
strClipboard = strClipboard + vbCrLf + Clipboard.GetText()
Catch ex As System.Runtime.InteropServices.ExternalException
Throw New Exception(ex.Message, ex.InnerException)
MessageBox.Show(”The Clipboard could not be accessed.”, “Please try again.”, MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End If
intRow += 1
Next
Clipboard.SetText(strClipboard)
End Sub
WindowsForms/Webforms?
WindowsForms -> You can trap all keypresses and keyeventcodes right.
WebForms -> You can again trap document.onKeyDown function in JavaScript and set the event.keyCode to zero.
When you copy data from Excel, the copied data is placed on clipboard as tab delimited data (you can verify this by copy some data some excel and paste in notepad)... So to paste that data to a DataGridView, you'll need to read the data from clipboard as a string, then work on that string to parse out the rows and columns, and then set the datagridview cells accordingly.
Now to go the other way, when you copy the data from your DGV, you need to get the values from the selected cells and build a tab delimited string, then place that string on clipboard. That way, you can paste it back to Excel or other DGV's.
Code:Public Sub PasteData(ByRef dgv As DataGridView)
Dim tArr() As String
Dim arT() As String
Dim i, ii As Integer
Dim c, cc, r As Integer
tArr = Clipboard.GetText().Split(Environment.NewLine)
r = dgv.SelectedCells(0).RowIndex
c = dgv.SelectedCells(0).ColumnIndex
For i = 0 To tArr.Length - 1
arT = tArr(i).Split(vbTab)
cc = c
For ii = 0 To arT.Length - 1
With dgv.Item(cc, r)
.Value = arT(ii).TrimStart
End With
cc = cc + 1
Next
r = r + 1
Next
End Sub