Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

VB.NET:  Word table from Datagridview, windows forms

Posted on 2011-09-30
8
521 Views
Last Modified: 2012-05-12
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
Comment
Question by:bgernon
  • 4
  • 4
8 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36894733
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
 

Author Comment

by:bgernon
ID: 36894781
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36900492
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:bgernon
ID: 36903711
The last row of the grid is the only row inserted into the table.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 36906086
>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
 

Author Comment

by:bgernon
ID: 36910515
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
 

Author Closing Comment

by:bgernon
ID: 36910523
CodeCruiser rocks!
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36912236
Glad to help :-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question