1

我有 3 个 vb.net 形式的 Datagridview 控件。DGV1 带有价格栏,DGV2 带有数量栏,第三个是总栏 DGV3。任何人都请告诉我如何执行此操作,DGV1*DGV2 在 DGV3 上显示总数,并在每次 DVG1 单元格值更改时更新 DGV3。我下面的代码不会更新 DGV3。此外,问题之一是 DGV3 在 DGV2 绑定 DATA 之前计算,如果 DGV2 单元格值 = 0,它会给我一个错误的总数。任何想法 ?赞赏,

 Private Sub DGV1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV1.CellValueChanged
    Dim Tot As Int32 = 0
   DGV3.Enabled = False
    DGV3.DataSource = Nothing
    DGV3.Enabled = True

    Dim OBJ As Double
    Dim SALES As Integer
    Dim dtt As DataTable
    For Each R As DataGridViewRow In Me.DGV1.Rows
        For Each N As DataGridViewRow In Me.DGV2.Rows

            OBJ = CDbl(R.Cells(4).Value)
            SALES = CInt(CDbl(N.Cells(0).Value))
            Tot = CInt(OBJ * SALES)
            DGV3.Rows.Add(Tot.ToString)

        Next
    Next
End Sub
4

2 回答 2

0

如果您能够使用一个 datagridview,我会这样做:

    Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ''// Set the number of columns.
        DataGridView1.ColumnCount = 3
        DataGridView1.ColumnHeadersVisible = True

        ''// Set the column header style. 
        Dim columnHeaderStyle As New DataGridViewCellStyle()

        columnHeaderStyle.BackColor = Color.Beige
        columnHeaderStyle.Font = New Font("Verdana", 10, FontStyle.Bold)
        DataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle

        ''// Set column names.
        DataGridView1.Columns(0).Name = "Price"
        DataGridView1.Columns(1).Name = "Quantity"
        DataGridView1.Columns(2).Name = "Total"

        DataGridView1.Enabled = False
        DataGridView1.DataSource = Nothing
        DataGridView1.Enabled = True

    End Sub

    Private Sub DataGridView1_CurrentCellChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellChanged

        ''// show the total in column 3 and update column 3 everytime column `price's` cellvalue gets changed.
        If (DataGridView1.CurrentCell.ColumnIndex = 0) Then
            ''// MsgBox("Updating column three `Total`")

            For Each R As DataGridViewRow In DataGridView1.Rows
                On Error Resume Next ''// iKNOW <-.->

                Dim price As Double = R.Cells(0).Value.ToString
                Dim quantity As Integer = R.Cells(1).Value.ToString

                ''// not the auto new new at the bottom. <`.'>
                If Not (R.IsNewRow) Then
                    Dim Tot As Double = CInt(price * quantity)
                    R.Cells(2).Value = Tot
                End If

            Next

        End If

     End Sub
 End Class
于 2013-04-14T01:50:01.683 回答
0

创建一个空 DGV 并使用添加列在其中添加所需的列。然后使用编辑列选择要在您现在创建的每个特定列下显示的值。您可以在每个新创建的列中看到 DataBindingsource 选项。所以选择你需要绑定的数据源和 DisplayMember (包含你想显示的值的列名)

于 2013-09-19T08:53:30.773 回答