Results 1 to 5 of 5

Thread: VB.NET copy from Datagrid & paste to another location?

  1. #1
    Join Date
    Feb 2009
    Posts
    7

    VB.NET copy from Datagrid & paste to another location?

    Hi,

    I want to know if this is possible to copy the data or records from a cell & then paste it somewhere else?

    What property should be used or I need to code something for this to make it possible?

  2. #2
    Join Date
    Jan 2009
    Posts
    38

    Re: VB.NET copy from Datagrid & paste to another location?

    You have to learn this for what you are asking!
    http://www.codeproject.com/KB/databa...opyHelper.aspx
    and
    http://www.codeproject.com/KB/webfor...rtToExcel.aspx
    I hope this helps you!

  3. #3
    Join Date
    Jan 2009
    Posts
    18

    Re: VB.NET copy from Datagrid & paste to another location?

    Copy datagridview selection to clipboard

    Code:
    Private Sub ctxCopyAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ctxCopyAll.Click
    
    CopyAllRowsToClipboard()
    
    End Sub
    
    Private Sub CopyAllRowsToClipboard()
    
    Dim intRow As Int32 = 0
    
    Dim drRow As DataGridViewRow = Nothing
    
    Dim strClipboard As String = String.Empty
    
    For Each drRow In grdBudgetData.Rows
    
    drRow.Selected = True
    
    grdBudgetData.SelectAll()
    
    If intRow = 0 Then
    
    grdBudgetData.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText
    
    Else
    
    grdBudgetData.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText
    
    End If
    
    If Me.grdBudgetData.GetCellCount(DataGridViewElementStates.Selected) > 0 Then
    
    Try
    
    ‘ Add the selection to the clipboard.
    
    Clipboard.SetDataObject(Me.grdBudgetData.GetClipboardContent())
    
    ‘ Replace the text box contents with the clipboard text.
    
    strClipboard = strClipboard + vbCrLf + Clipboard.GetText()
    
    Catch ex As System.Runtime.InteropServices.ExternalException
    
    Throw New Exception(ex.Message, ex.InnerException)
    
    MessageBox.Show(”The Clipboard could not be accessed.”, “Please try again.”, MessageBoxButtons.OK, MessageBoxIcon.Information)
    
    End Try
    
    End If
    
    intRow += 1
    
    Next
    
    Clipboard.SetText(strClipboard)
    
    End Sub

  4. #4
    Join Date
    Feb 2009
    Posts
    7

    Re: VB.NET copy from Datagrid & paste to another location?

    WindowsForms/Webforms?

    WindowsForms -> You can trap all keypresses and keyeventcodes right.
    WebForms -> You can again trap document.onKeyDown function in JavaScript and set the event.keyCode to zero.

  5. #5
    Join Date
    May 2008
    Posts
    72

    Re: VB.NET copy from Datagrid & paste to another location?

    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.

    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

Similar Threads

  1. Replies: 5
    Last Post: 26-04-2012, 04:16 AM
  2. Unable to copy and paste in mac
    By NSA_CIA in forum Operating Systems
    Replies: 3
    Last Post: 17-11-2009, 01:46 PM
  3. How do i Copy and Paste MS-DOS text ?
    By SamDust in forum Customize Desktop
    Replies: 2
    Last Post: 03-04-2009, 01:18 PM
  4. Replies: 0
    Last Post: 18-03-2009, 10:00 PM
  5. Copy and Paste does not work
    By M.Scarlet in forum Microsoft Project
    Replies: 1
    Last Post: 06-02-2008, 03:46 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,623,534.60744 seconds with 17 queries