|
-
Feb 13th, 2008, 11:43 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [2005] DataGridView - Paste?
Hello!
I know I can allow the data from the datagridview to be copiedand pasted somewhere else.
But, how do I allow data to be pasted into the datagridview?
I have instances where someone might copy one cell and paste the value to another cell or might copy data from excel and want to paste into the datagridview.
I googled it and only came across that it couldn't be done.
Hoping that there might be a way to do it.
Thanks in advance!
-
Feb 13th, 2008, 02:41 PM
#2
Re: [2005] DataGridView - Paste?
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.
-
Feb 13th, 2008, 03:39 PM
#3
Thread Starter
Hyperactive Member
Re: [2005] DataGridView - Paste?
Thanks, i was able to use this code to make it work the way i want it to:
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
-
Mar 24th, 2008, 11:52 PM
#4
PowerPoster
Re: [RESOLVED] [2005] DataGridView - Paste?
How to use your code and what event should be triggered?
-
Mar 25th, 2008, 12:13 PM
#5
Thread Starter
Hyperactive Member
Re: [RESOLVED] [2005] DataGridView - Paste?
Hello! I did this:
Code:
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
-
Mar 26th, 2008, 12:21 AM
#6
PowerPoster
Re: [RESOLVED] [2005] DataGridView - Paste?
I tried putting a datagridview on the form and named it as dgvSRs and have tried this code below. Run the proj. copied a range of values from on column in excel and pasted it in the datagridview but only the number 1 is pasted in the datagrid view column not all of the values 1,2,3,4,5,6 are pasted plus i get this error in the immediate window while the project is running. A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
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
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
End Class
-
Mar 26th, 2008, 07:13 AM
#7
Thread Starter
Hyperactive Member
Re: [RESOLVED] [2005] DataGridView - Paste?
Ah! Now that you mention it, after i posted this, a couple weeks later i did run into that issue and had to update the code:
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
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
-
Mar 27th, 2008, 07:15 PM
#8
PowerPoster
Re: [RESOLVED] [2005] DataGridView - Paste?
If I right click the mouse and use paste its not pasting still all the values i copied from excel. Only the number 1 is pasted in the column. But if I used the keyboard then that's the time values are pasted.
Have you tried coding for the right click of the mouse?
-
Mar 27th, 2008, 09:13 PM
#9
PowerPoster
Re: [RESOLVED] [2005] DataGridView - Paste?
Since this thread is already resolved. I opened up another thread for other issues about copy and paste and it's in this thread.
-
Apr 15th, 2008, 02:02 AM
#10
PowerPoster
Re: [RESOLVED] [2005] DataGridView - Paste?
HI! I tried using your code above but i can only paste 1 value in column1 the rest of the values are not pasted. I dont know what happened...it was working before but now it's not anymore.????
Code:
Public Class Form1
Private Sub KeyPaste(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
If e.KeyCode = Keys.V And Keys.ControlKey Then
Paste()
End If
End Sub
Public Sub Paste()
PasteData(DataGridView1)
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
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
|