Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 539
  • Last Modified:

VB.NET: Word table from Datagridview, windows forms

I've successly used the attached code for 3 datagridviews in another window form.  For whatever reason, it does not want to work so well on the form I am currently working on.

The output in the word table only shows the last row of the datagridview.  Don't know where the preceding rows go. I think it may have something to do with emply or null datagridview cells.  I checked for null with an if statement and the output was 'Null'.

I had also checked the values with a messagebox.  The values were there.

   
wdDoc.Bookmarks.Item("\endofdoc").Range.InsertParagraphAfter()            
            Dim dgTable As Word.Table  'Grid Headers
            Dim dgvTableColumns = DataGridView1.ColumnCount
            Dim dgvTableRows = DataGridView1.RowCount
            If dgvTableRows >= 1 Then
                dgTable = wdDoc.Tables.Add(wdDoc.Bookmarks.Item("\endofdoc").Range, 1, dgvTableColumns)
                With dgTable
                    .Columns(1).Width = 125
                    .Columns(2).Width = 75
                    .Columns(3).Width = 125
                    .Columns(4).Width = 80
                    .Columns(5).Width = 100
                    .Columns(6).Width = 100
                End With
                For ch As Integer = 1 To dgvTableColumns
                    dgTable.Cell(1, ch).Range.Font.Bold = True
                    dgTable.Cell(1, ch).Range.Text = DataGridView1.Columns(ch - 1).HeaderText.ToString
                Next

                wdDoc.Bookmarks.Item("\endofdoc").Range.InsertParagraphAfter()

                dgTable = wdDoc.Tables.Add(wdDoc.Bookmarks.Item("\endofdoc").Range, 1, dgvTableColumns) 'Grid cells
                For r = 1 To dgvTableRows                    
                  For c = 1 To dgvTableColumns
                        With dgTable
                            .Columns(1).Width = 125
                            .Columns(2).Width = 75
                            .Columns(3).Width = 125
                            .Columns(4).Width = 80
                            .Columns(5).Width = 100
                            .Columns(6).Width = 100
                        End With
                        If (DataGridView1(c - 1, r - 1).Value.Equals(System.DBNull.Value)) Then
                            dgTable.Cell(r, c).Range.Text = "Null"
                        Else
                            dgTable.Cell(r, c).Range.Text = DataGridView1(c - 1, r - 1).Value.ToString
                        End If  
                  Next
                Next

Open in new window

0
bgernon
Asked:
bgernon
  • 4
  • 4
1 Solution
 
CodeCruiserCommented:
Have you stepped through the code to see what its doing?

By the way, following lines do not need to be inside the loop

With dgTable
                            .Columns(1).Width = 125
                            .Columns(2).Width = 75
                            .Columns(3).Width = 125
                            .Columns(4).Width = 80
                            .Columns(5).Width = 100
                            .Columns(6).Width = 100
                        End With
0
 
bgernonAuthor Commented:
Yes, I know that.  But I've been fussing with the code so much, I left them in by mistake.  You can see that I did that with the headers.  

I put a messagebox in and the right values came up.  I stepped through the code, and I can see the values.  But all I end up with is the last datagrid row.

0
 
CodeCruiserCommented:
So the last row of grid is inserted into the last row of table? Or last row of grid is in the first row of table?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bgernonAuthor Commented:
The last row of the grid is the only row inserted into the table.
0
 
CodeCruiserCommented:
>dgTable = wdDoc.Tables.Add(wdDoc.Bookmarks.Item("\endofdoc").Range, 1, dgvTableColumns)

Could it be because you have 1 as number of rows when adding table? Try

dgTable = wdDoc.Tables.Add(wdDoc.Bookmarks.Item("\endofdoc").Range, dgvTableRows, dgvTableColumns)
0
 
bgernonAuthor Commented:
Well, color me embarressed.  Somehow I got it into my head that I meant the first column.  So 1 to how many columns there were.  

As my supervisor always says, it's good to have a second set of eyes.  

Thanks CodeCruiser.
0
 
bgernonAuthor Commented:
CodeCruiser rocks!
0
 
CodeCruiserCommented:
Glad to help :-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now