Reputation: 120
I have a datagridview on a form with 6 columns and an undetermined number of rows. The user is entering values into this datagridview. I would now like to create an invoice based on the entries in the datagridview. I am creating this invoice on another form. Unfortunately, the values in the cells are not showing up with the code I have. This is the code located in the Invoice class.
Dim lbl(5) As Label
For Each row As DataGridViewRow In Main.DataGridView1.Rows
If Not row.IsNewRow Then
Dim x As Integer = 0
For Each col As DataGridViewColumn In Main.DataGridView1.Columns
i = row.Index
j = col.Index
With lbl(x)
.AutoSize = True
.BackColor = System.Drawing.SystemColors.Control
.Font = New Font(lbl(x).Font.FontFamily, 8.45, FontStyle.Regular)
.ForeColor = System.Drawing.SystemColors.ControlText
.Location = New System.Drawing.Point(i * 111 + 6, (i + 1) * 24 + 16)
.Text = Main.DataGridView1.Rows(i).Cells(j).Value
End With
MsgBox(lbl(x).Text)
GroupBoxInvoiceInvoice.Controls.Add(lbl(x))
x += 1
Next
End If
Next
Any help would be greatly appreciated!
Here is the new block of code.
Dim Label1, Label2, Label3, Label4, Label5, Label6 As New Label
Dim lbl() As Control = {Label1, Label2, Label3, Label4, Label5, Label6}
Dim val() As String = {"Date", "Category", "Description", "Units", "Rate", "Amount"}
For Each row As DataGridViewRow In Main.DataGridView1.Rows
If Not row.IsNewRow And i < Main.DataGridView1.Rows.Count Then
Dim x As Integer = 0
For Each col As DataGridViewColumn In Main.DataGridView1.Columns
i = row.Index
j = col.Index
With lbl(x)
.AutoSize = True
.BackColor = System.Drawing.SystemColors.Control
.Font = New Font(lbl(x).Font.FontFamily, 8.45, FontStyle.Regular)
.ForeColor = System.Drawing.SystemColors.ControlText
.Location = New System.Drawing.Point(j * 111 + 6, (i + 1) * 24 + 16)
.Text = Main.DataGridView1.Rows(i).Cells(j).Value
End With
GroupBoxInvoiceInvoice.Controls.Add(lbl(x))
x += 1
j += 1
Next
i += 1
j = 0
x = 0
End If
Next
Thank you, Plutonix!! You're help has been awesome and greatly appreciated! You have shown much patience, also! Here is the final portion of code that worked for me based off of what you had shown below. Thanks again!!
i = 0
For Each row As DataGridViewRow In Main.DataGridView1.Rows
If Not row.IsNewRow Then
For c As Integer = 0 To row.Cells.Count - 3
Dim lbl As New Label
With lbl
.AutoSize = True
.BackColor = System.Drawing.SystemColors.Control
.Font = New Font(lbl.Font.FontFamily, 8.45, FontStyle.Regular)
.ForeColor = System.Drawing.SystemColors.ControlText
.Location = New System.Drawing.Point(c * 111 + 6, (i + 1) * 24 + 16)
.Text = row.Cells(c).Value.ToString
End With
GroupBoxInvoiceInvoice.Controls.Add(lbl)
Console.WriteLine("label {0}", row.Cells(c).Value.ToString)
Next
End If
i += 1
Next
Upvotes: 0
Views: 1600
Reputation: 38875
You have 1 or 2 problems with your loop.
Dim lbl(5) As Label
This declares you plan to make an array of labels, but you never initialize it. When I try to run your code (w/o dgv) it crashes because there is nothing in lbl(x) ('Object reference not set to an instance of an object.'). There is no reason to stash the labels in an array if they are going to be added to a form. So, just create labels as you need them:
For Each col As DataGridViewColumn In Main.DataGridView1.Columns
i = row.Index
j = col.Index
Dim lbl As New Label ' make a new label
With lbl
.AutoSize = True
.BackColor = System.Drawing.SystemColors.Control
.Font = New Font(lbl(x).Font.FontFamily, 8.45, FontStyle.Regular)
.ForeColor = System.Drawing.SystemColors.ControlText
' the above props are being set to default values, so arent really needed
.Location = New System.Drawing.Point(i * 111 + 6, (i + 1) * 24 + 16)
' I think this needs to be changed:
.Text = Main.DataGridView1.Rows(i).Cells(j).Value.ToString
End With
MsgBox(lbl(x).Text)
GroupBoxInvoiceInvoice.Controls.Add(lbl)
x += 1
Next
This worked fine without the dgv reference, so if the labels are still blank, check to make sure there is something in those cells to display. If that loop was executing all the way something somewhere was eating the exception and could be eating another.
-------------------- EDIT ----------------------------
The problem is using row.index and col.index as for i,j. Those have to do with databands and return 0 otherwise. You want to loop thru the CELLS for each row:
For Each row As DataGridViewRow In dgv.Rows
If row.IsNewRow = False Then
' loop thru cells in row
For c As Integer = 0 To rrow.Cells.Count - 1
Dim lbl As New Label
With lbl
.AutoSize = True
.BackColor = System.Drawing.SystemColors.Control
.BorderStyle = BorderStyle.Fixed3D
.ForeColor = System.Drawing.SystemColors.ControlText
.Location = New System.Drawing.Point(r + 5, c * 22) ' FIX
' CONVERT value TO STRING!!!
.Text = rrow.Cells(c).Value.ToString
End With
Panel1.Controls.Add(lbl)
' prints as many time as there are labels/cells
Console.WriteLine("label {0}", rrow.Cells(c).Value.ToString)
Next
End If
Next
Console.WriteLine("{0}", Panel1.Controls.Count) ' my locations are foobar
' but the right number are there.
You will want to add r,c (or i, j) counters for positioning the labels on the form, but otherwise, all is well in LabelLand...of should be
Upvotes: 0