Results 1 to 5 of 5

Thread: Export DGV to Word with column headers

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2024
    Posts
    10

    Export DGV to Word with column headers

    Hello

    How can I export the Datagridview with column headers to Word???
    Does anyone have an example code for me?

    Thanks in advance....
    Last edited by jmcilhinney; Jul 25th, 2024 at 10:48 PM. Reason: Translated to English

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

    Re: Export DGV to Word with column headers

    This is an English-only site. Please post only in English. I have translated your post and title to English this time. Any future threads posted in another language will be closed.
    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,665

    Re: Export DGV to Word with column headers

    What have you done and where are you stuck?
    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2024
    Posts
    10

    Re: Export DGV to Word with column headers

    Hello, I got this code from an online forum.
    I use it to export the DGV2 to Word, but the header texts are not included. According to the forum it should work but not for me.



    Code:
    Dim r As Integer, c As Integer
    Dim oTable As Word.Table = objDoc.Tables.Add(objDoc.Bookmarks.Item("\endofdoc").Range,
    Datagridview2.RowCount, Datagridview2.ColumnCount, Datagridview2.ColumnHeadersVisible = True)
    
    
    
    For r = 0 To Datagridview2.RowCount - 2
        For c = 0 To Datagridview2.Columns.Count - 1
    
            oTable.Cell(r + 1, c + 1).Range.Text = Datagridview2(c, r).Value.ToString()
    
    
            With objDoc.Tables(1)
    
    
                .ApplyStyleHeadingRows = False
                .ApplyStyleLastRow = True
                .ApplyStyleFirstColumn = True
                .ApplyStyleLastColumn = True
                .ApplyStyleRowBands = False
                .ApplyStyleColumnBands = False
                .Columns(1).Width = 20
                .Columns(2).Width = 46
                .Columns(3).Width = 300
                .Columns(4).Width = 70
                .Columns(5).Width = 70
    
            End With

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,665

    Re: Export DGV to Word with column headers

    So your question is effectively how to get the column header text from the grid. This is why it's important to provide a FULL and CLEAR explanation of the actual problem. Someo9ne could have wasted a lot of time telling you stuff that you already know. Each column has a HeaderText property. Seems useful, no? You need to loop through the columns, which you know how to do because you're already doing it, and get that property value from each one.
    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

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