我在一个有 6 列和不确定行数的表单上有一个 datagridview。用户正在将值输入到这个 datagridview 中。我现在想根据 datagridview 中的条目创建发票。我正在另一种形式上创建此发票。不幸的是,单元格中的值没有显示在我的代码中。这是位于 Invoice 类中的代码。
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
任何帮助将不胜感激!
这是新的代码块。
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
谢谢你,冥王星!!您的帮助非常棒,非常感谢!你也表现出了很大的耐心!根据您在下面显示的内容,这是对我有用的代码的最后一部分。再次感谢!!
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