添加2个datagridview单元格值

时间:2013-04-13 23:28:10

标签: vb.net

我在vb.net表单中有3个Datagridview控件。带价格列的DGV1,带数量栏的DGV2和第3列的DGV3总列。 deos任何人都告诉我如何做到这一点,DGV1 * DGV2显示DGV3上的总数,并在每次更改DVG1单元格值时更新DGV3。我的代码下面没有更新DGV3。另外,问题之一是在DGV2绑定DATA之前计算DGV3,如果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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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