Results 1 to 10 of 10

Thread: [RESOLVED] [2005] DataGridView - Paste?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Posts
    406

    Resolved [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!

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Posts
    406

    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

  4. #4
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: [RESOLVED] [2005] DataGridView - Paste?

    How to use your code and what event should be triggered?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Posts
    406

    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

  6. #6
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    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
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Posts
    406

    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

  8. #8
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    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?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  9. #9
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    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.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  10. #10
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    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
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width