Copy Datagridview column to clipboard

Hi,
How can I copy datagridview column to clipboard .

Cheers
RIASAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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
NorieAnalyst Assistant Commented:
RIAS

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

Thanks
0
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.