Click here to Skip to main content
15,885,914 members
Articles / Productivity Apps and Services / Microsoft Office / Microsoft Excel
Tip/Trick

Copying DataGridView Contents To Clipboard

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
9 Nov 2012CPOL 48K   6   4
Copy DataGridView contents to clipboard regardless of current selection.

Introduction 

This code snippet copies the entire contents of a DataGridView to clipboard, including headers, respecting the displayed column order and excluding invisible columns - and most importantly - irrespective of what is currently selected in the grid.

Background

The DataGridView allows you to copy what is selected in the grid to the clipboard using its GetClipboardContent method. I needed to provide a means of copying the entire grid's contents irrespective of what was selected. The code is quite simple but I spent too long trying to find it without success so thought I'd post it to save the next person. 

Using the code

Add the following method to your code, pass in the reference to your DataGridView and that's it. 

VB
Private Sub CopyDataGridViewToClipboard(ByRef dgv As DataGridView)
    Dim s As String = ""
    Dim oCurrentCol As DataGridViewColumn    'Get header
    oCurrentCol = dgv.Columns.GetFirstColumn(DataGridViewElementStates.Visible)
    Do
      s &= oCurrentCol.HeaderText & Chr(Keys.Tab)
      oCurrentCol = dgv.Columns.GetNextColumn(oCurrentCol, _
         DataGridViewElementStates.Visible, DataGridViewElementStates.None)
    Loop Until oCurrentCol Is Nothing
    s = s.Substring(0, s.Length - 1)
    s &= Environment.NewLine    'Get rows
    For Each row As DataGridViewRow In dgv.Rows
      oCurrentCol = dgv.Columns.GetFirstColumn(DataGridViewElementStates.Visible)
      Do
        If row.Cells(oCurrentCol.Index).Value IsNot Nothing Then
          s &= row.Cells(oCurrentCol.Index).Value.ToString
        End If
        s &= Chr(Keys.Tab)
        oCurrentCol = dgv.Columns.GetNextColumn(oCurrentCol, _
              DataGridViewElementStates.Visible, DataGridViewElementStates.None)
      Loop Until oCurrentCol Is Nothing
      s = s.Substring(0, s.Length - 1)
      s &= Environment.NewLine
    Next    'Put to clipboard
    Dim o As New DataObject
    o.SetText(s)
    Clipboard.SetDataObject(o, True)
End Sub

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralCool Tip Pin
Brad Joss11-Feb-19 16:37
professionalBrad Joss11-Feb-19 16:37 
QuestionCopying DataGridView Contents To Clipboard Pin
Member 126027838-Jun-17 4:57
Member 126027838-Jun-17 4:57 
SuggestionEasier way... Pin
AdamWhitehat27-Sep-14 7:28
professionalAdamWhitehat27-Sep-14 7:28 
AnswerAnother Easier way... Pin
Xelazed423-Mar-15 14:02
Xelazed423-Mar-15 14:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.