|
-
Mar 27th, 2008, 09:11 PM
#1
Thread Starter
PowerPoster
[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
Last edited by Simply Me; Mar 27th, 2008 at 09:16 PM.
-
Mar 31st, 2008, 06:29 PM
#2
New Member
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!
-
May 5th, 2008, 10:23 PM
#3
Thread Starter
PowerPoster
Re: [2005]Datagridview Copy and Paste
I almost forgot this thread. Any other idea here please?
-
May 6th, 2008, 01:09 AM
#4
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.
-
Jun 28th, 2008, 03:26 AM
#5
New Member
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
Last edited by Tomix; Jun 28th, 2008 at 06:15 AM.
-
Dec 11th, 2008, 01:31 PM
#6
Hyperactive Member
Re: [2005]Datagridview Copy and Paste
Nice adjustments!
I had added the pice to add more rows and i like the other changes.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|