Results 1 to 3 of 3

Thread: datagridview getclipboardcontent paste has blank first row column

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    datagridview getclipboardcontent paste has blank first row column

    I have a app that is populating a datagridview and have a button to highlight and copy all the data in the datagridview. When I go and paste into excel the first row and first column are blank. Is there a easy switch to not include this?

    Here is the code testing with.
    Code:
    Private Sub btnCopyAll_Click(sender As System.Object, e As System.EventArgs) Handles btnCopyAll.Click
            Dim sClipboard As String = String.Empty
    
            dgvResults.SelectAll()
            dgvResults.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText
    
            If Me.dgvResults.GetCellCount(DataGridViewElementStates.Selected) > 0 Then
                Try
                    ' Add the selection to the clipboard.
                    Clipboard.SetDataObject(Me.dgvResults.GetClipboardContent())
    
                    ' Replace the text box contents with the clipboard text.
                    sClipboard = sClipboard + 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
    
            Clipboard.SetText(sClipboard)
    
        End Sub

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: datagridview getclipboardcontent paste has blank first row column

    Well you have to make some decisions. If you include headers then that means a space is also required for row headers, hence the spare column. The spare row is introduced by the procedure in which you first copy the data, read it back to a string and then copy the string, of which I really can't see any necessity anyway.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    Re: datagridview getclipboardcontent paste has blank first row column

    Thanks. That helped me figured it out.

    1) I had a vbCrLf that was adding the space above the header. Removed that
    2) I needed to set the RowHeaderVisible to false. I was only thinking column header but guess you have two headers.

    Code:
        Private Sub btnCopyAll_Click(sender As System.Object, e As System.EventArgs) Handles btnCopyAll.Click
            Dim sClipboard As String = String.Empty
            dgvResults.AutoResizeColumns()
            dgvResults.RowHeadersVisible = False
            dgvResults.SelectAll()
    
            dgvResults.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText
    
            If Me.dgvResults.GetCellCount(DataGridViewElementStates.Selected) > 0 Then
                Try
                    ' Add the selection to the clipboard.
                    Clipboard.SetDataObject(Me.dgvResults.GetClipboardContent())
    
                    ' Replace the text box contents with the clipboard text.
                    sClipboard = 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
    
            Clipboard.SetText(sClipboard)
    
        End Sub

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