Solved

VB.NET:  Word table from Datagridview, windows forms

Posted on 2011-09-30
8
512 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Multiple file Upload asp.net 2 41
Convert to Hour/minute textbox and compare 3 54
Get Top Visible Row of Datagridview 6 28
Vb. Net application freezes 9 31
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

25 Experts available now in Live!

Get 1:1 Help Now