Solved

VB.NET:  Word table from Datagridview, windows forms

Posted on 2011-09-30
8
530 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
[X]
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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Technology Partners: 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!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

615 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