Solved

Copying a DataGridView and all it's cells to the Clipboard?

Posted on 2014-01-29
3
274 Views
Last Modified: 2014-01-29
I know that the DataGridView is supposed to be able to automatically copy and paste cells into a different program such as Excel, Notepad, etc. However, I am unable to do so. How is this done or is a custom routine needed?

Thanks!
0
Comment
Question by:BlakeMcKenna
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
David L. Hansen earned 500 total points
Comment Utility
This should do it:

Private Sub CopyDGVToClipboard(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

Open in new window

Please note: the above code is taken from CodeProject, here:
http://www.codeproject.com/Tips/491462/Copying-DataGridView-Contents-To-Clipboard
0
 

Author Closing Comment

by:BlakeMcKenna
Comment Utility
That worked great sl8rz...thanks!
0
 
LVL 15

Expert Comment

by:David L. Hansen
Comment Utility
Sure..you bet. :)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now