0

我在一个有 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
4

1 回答 1

0

您的循环有 1 或 2 个问题。

Dim lbl(5) As Label

这表明您计划制作一个标签数组,但您从未初始化它。当我尝试运行您的代码(不带 dgv)时,它会崩溃,因为 lbl(x) 中没有任何内容(“对象引用未设置为对象的实例。”)。如果要将标签添加到表单中,则没有理由将标签存储在数组中。因此,只需根据需要创建标签:

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

这在没有 dgv 参考的情况下工作得很好,所以如果标签仍然是空白的,请检查以确保这些单元格中有要显示的内容。如果该循环一直在执行某处某处正在吃异常并且可能正在吃另一个。

- - - - - - - - - - 编辑 - - - - - - - - - - - - - -

问题在于 i,j 使用 row.index 和 col.index。这些与数据带有关,否则返回 0。您想循环遍历每一行的 CELLS:

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.

您将需要添加 r、c(或 i、j)计数器来定位表单上的标签,但除此之外,LabelLand 中的一切都很好......应该是

于 2013-10-09T21:10:52.797 回答