Copy Datagridview column to clipboard

Hi,
How can I copy datagridview column to clipboard .

Cheers
RIASAsked:
Who is Participating?
 
Fernando SotoRetiredCommented:
Hi RIAS;

See if this is more of what you are looking for?
Imports System.Text

DataGridView1.DataSource = YouDataSource
'' Column SortMode must be something other then the default.
For Each col As DataGridViewColumn In DataGridView1.Columns
    col.SortMode = DataGridViewColumnSortMode.Programmatic
Next
'' Clicking on header selects column 
DataGridView1.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect

'' After selecting the column and wanting to copy to the clipboard do this
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    '' Get the column the user selected
    Dim col As DataGridViewSelectedColumnCollection = DataGridView1.SelectedColumns
    '' Used to write the column data to be copied to the clipboard
    Dim columnInfo As New StringBuilder
    Dim nl As String = vbNewLine

    '' Get the column text 
    If col IsNot Nothing Then
        Dim idx As Integer = col.Item(0).Index
        columnInfo.AppendFormat("Column : {0}" & nl, DataGridView1.Columns.Item(idx).Name)
        For Each row As DataGridViewRow In DataGridView1.Rows
            columnInfo.Append(row.Cells.Item(idx).Value & nl)
        Next
    End If
    
    '' Post the text to the clipboard
    If columnInfo.Length > 0 Then
        Clipboard.SetText(columnInfo.ToString())
    End If

End Sub

Open in new window

0
 
Ayoub RouziCeo & CoFounderCommented:
Hello,

Please check this out and give me your feedback:

<BrowsableAttribute(True)>
Public Property ClipboardCopyMode As DataGridViewClipboardCopyMode

Open in new window

0
 
RIASAuthor Commented:
Hello,
i am using vb.net desktop , is there a code on the grid cell click event ?

Thanks
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Ayoub RouziCeo & CoFounderCommented:
I am guessing that you have to set MultiSelect property of the DataGridView to true

Also maybe you should set ClipboardCopyMode to Disable. Change this property to any other three available options.

Another choice, this problem could maybe be due to solidWorks.Interop.sldworks and the application being in Solidworks environment. I've heard from other solidworks developers the ctrl C just refused to work
0
 
RIASAuthor Commented:
Nice thanks will check if I have this option and brb.
0
 
RIASAuthor Commented:
Ayoub ,
Tried it but it wont copy the column contents.
Any code suggestions?
0
 
Ayoub RouziCeo & CoFounderCommented:
RIAD, please try:

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

Open in new window


Note: The DataGridView allows you to copy what is selected in the grid to the clipboard using its GetClipboardContent method.
0
 
RIASAuthor Commented:
Great! Thanks will try and brb
0
 
RIASAuthor Commented:
Ayoub ,
Tried it, it works thanks but, it selects all columns , is there any way only the clicked column is selected ?

Cheers
0
 
Ayoub RouziCeo & CoFounderCommented:
Hi, I don't think it's possible.

Thank you
0
 
RIASAuthor Commented:
No worries Ayoub , Thanks for your help.
0
 
NorieVBA ExpertCommented:
RIAS

Which column do you want to copy to the clipboard?
0
 
RIASAuthor Commented:
The column Selected by the user.

Thanks
0
 
RIASAuthor Commented:
Thanks Fernando! Will try and brb
0
 
RIASAuthor Commented:
Hello,
Ayoub solution will be the assisted solution. Cannot find the option to mark it as assisted .

Thanks
1
 
Fernando SotoRetiredCommented:
Not a problem RIAS, glad to help.
0
 
RIASAuthor Commented:
Thanks Fernando, was struggling with this bit for quite sometime.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.