Results 1 to 6 of 6

Thread: [2005]Datagridview Copy and Paste

  1. #1

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    [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.
    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

  2. #2
    New Member
    Join Date
    Mar 2008
    Posts
    1

    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!

  3. #3

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: [2005]Datagridview Copy and Paste

    I almost forgot this thread. Any other idea here please?
    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    New Member
    Join Date
    Jun 2008
    Posts
    1

    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.

  6. #6
    Hyperactive Member
    Join Date
    Jul 2003
    Posts
    406

    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
  •  



Click Here to Expand Forum to Full Width