[2005]Datagridview Copy and Paste
I opened this thread to ask some other issues regarding copy and paste in datagridview. This thread is based on this thread. the work of jeffcravener is working but i have some other concerns to wit:
1. Use the mouse right click button as an alternative way of pasting instead of just only using the keyboard.
2. If the datagridview has only one row, when pasting more than one value from excel, the datagrid will automatically create the rest of the row.
3. on load of the form, the datagridview's first cell is highlighted by default, How do make it display the blinking bar (|) without double clicking the cell.
Thanks in advance!
Code:
Public Class Form1
Private Sub KeyPaste(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgvSRs.KeyDown
If e.KeyCode = Keys.V And Keys.ControlKey Then
Paste()
End If
End Sub
Public Sub Paste()
PasteData(dgvSRs)
End Sub
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
If tArr(i) <> "" Then
arT = tArr(i).Split(vbTab)
cc = c
For ii = 0 To arT.Length - 1
If cc > dgv.ColumnCount - 1 Then Exit For
If r > dgv.Rows.Count - 1 Then Exit Sub
With dgv.Item(cc, r)
.Value = arT(ii).TrimStart
End With
cc = cc + 1
Next
r = r + 1
End If
Next
End Sub
End Class
Re: [2005]Datagridview Copy and Paste
If you want to use mouse right click to paste the data, create a context menu strip on your form with DropDownItem "Paste", then for the click event for this item simply have PasteData(dgvSRs). hope this works!
Re: [2005]Datagridview Copy and Paste
I almost forgot this thread. Any other idea here please?
Re: [2005]Datagridview Copy and Paste
If you want to begin an editing session in the current cell you call the grid's BeginEdit method.
Re: [2005]Datagridview Copy and Paste
I have used this script and make some modifications to it. Now it can add rows to a table according the length of a clipboard.
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().Trim().Split(Environment.NewLine) '!!! Added Trim() ...because when pasting from vb App
r = dgv.CurrentCellAddress.Y() 'this is easier
c = dgv.CurrentCellAddress.X()
If (tArr.Length > (dgv.Rows.Count - r)) Then dgv.Rows.Add(tArr.Length - (dgv.Rows.Count - r)) 'check length of the clipboard and the datagridview
For i = 0 To tArr.Length - 1
If tArr(i) <> "" Then
arT = tArr(i).Split(vbTab)
cc = c
For ii = 0 To arT.Length - 1
If cc > dgv.ColumnCount - 1 Then Exit For
If r > dgv.Rows.Count - 1 Then Exit Sub
With dgv.Item(cc, r)
.Value = arT(ii).TrimStart
End With
cc = cc + 1
Next
r = r + 1
End If
Next
End Sub
Re: [2005]Datagridview Copy and Paste
Nice adjustments!
I had added the pice to add more rows and i like the other changes.