Solved

VB.NET:  Word table from Datagridview, windows forms

Posted on 2011-09-30
8
510 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
 

Author Comment

by:bgernon
ID: 36903711
The last row of the grid is the only row inserted into the table.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now