Results 1 to 4 of 4

Thread: copy / past datagridview content with column names into Excel

  1. #1

    Thread Starter
    Hyperactive Member fabianus's Avatar
    Join Date
    Sep 2004
    Location
    Paris
    Posts
    402

    copy / past datagridview content with column names into Excel

    Hello guys !

    I would like to copy the content of a datagridview with the column names in the clipboard and then past it into Excel. You may copy/past with datagridviews, but the column names are not copied. Does anybody have an idea how to do this ?

    Thanks a lot for any feedback !

    Regards,
    Fabianus

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: copy / past datagridview content with column names into Excel

    You'd have to see what format the data is copied to the Clipboard in, then construct such a format that included the column headers. Try copying data from a DGV normally and then call Clipboard.GetDataObject to get the copied data as an IDataObject. You can then call GetDataFormats to see what formats the data is available in and GetData to get the data in each of those formats. Once you understand the format of the data, then you can write code to bundle the contents of the DGV, including column headers, into an apropriate format and copy it to the Clipboard yourself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member fabianus's Avatar
    Join Date
    Sep 2004
    Location
    Paris
    Posts
    402

    Re: copy / past datagridview content with column names into Excel

    Hey jmcilhinney !

    Thanks a lot - as always your way solved my problem :-)

    Here is how I did it :

    Code:
            Dim ido As IDataObject = Clipboard.GetDataObject
            Dim t As String() = ido.GetFormats
            If t.Count > 2 Then
    
                Clipboard.SetData("UnicodeText", "nothing" & Chr(9) & "StartMoment" & Chr(9) & "SellTotalWeight" & Chr(9) & "SellPerItemInP" & Chr(9) & "SellPerWeightInKg" & Chr(9) & "SellRecordCount" & Chr(9) & "SellOrderCount" & Chr(9) & "SumTurnoverEurWithVAT" & Chr(9) & "SumTurnoverEurNoVAT" & Chr(9) & "SumProfitEur" & Chr(9) & "MeanPricePerKg" & Chr(9) & "LossTotalWeight" & Chr(9) & "LossPerItemInP" & Chr(9) & "LossPerWeightInKg" & Chr(9) & "LossRecordCount" & Chr(9) & "UnsoldTotalWeight" & Chr(9) & "UnsoldPerItemInP" & Chr(9) & "UnsoldPerWeightInKg" & Chr(9) & "UnsoldRecordCount" & Chr(13) & Chr(10) & ido.GetData(t(3)))
    
            End If
    Thanks again for you precious suggestions !

    Regards,
    Fabianus

  4. #4

    Thread Starter
    Hyperactive Member fabianus's Avatar
    Join Date
    Sep 2004
    Location
    Paris
    Posts
    402

    Re: copy / past datagridview content with column names into Excel

    Hey jmcilhinney,

    I just discovered the "ClipboardCopyMode" property of the DataGridView : simply set it to EnableAlwaysIncludeHeaderText and the column's header is copied, too :-)

    See you,
    Fabianus

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