运行时数据表更新错误

时间:2015-07-14 06:42:21

标签: vb.net datagridview

我有一个带有复选框列的datagridview。当我在复选框字段上单击(选中/取消选中)作为随机时,应在数据表(声明的运行时)中添加或删除相应行中的其他两个字段。

这样我就可以对数据表中的数据执行一些过程。 为此,我已将数据表声明为全局。

现在的问题是,每当我点击一个复选框时,需要一个简单的鼠标滚动来更新数据表,或者在新数据网格中需要一个点击来显示数据表中的值。

我的代码如下,

全球声明:Public PaymentTable As DataTable

Private Sub ShowOrdersFrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    DataBind()
    Me.DGVOrders.RowsDefaultCellStyle.BackColor = Color.GhostWhite
    Me.DGVOrders.AlternatingRowsDefaultCellStyle.BackColor = Color.PaleGoldenrod
    PaymentTable = New DataTable()
    PaymentTable.Columns.Add("RowId", GetType(Integer))
    PaymentTable.Columns.Add("Amount", GetType(Decimal))

End Sub
Private Sub DataBind()
    DGVOrders.DataSource = Nothing
    DGVOrders.Columns.Clear()
    Dim cmd As New SqlCommand
    Dim da As New SqlDataAdapter
    Dim dt As New DataTable
    con.Open()
    With cmd
        .Connection = con
        .CommandText = "select * from VIEW_PAYMENTS_DUES_BYORDER where CustCode='" & CustCode & "' order by OrderNo DESC"
    End With
    da.SelectCommand = cmd
    da.Fill(dt)
    BindingSource1.DataSource = dt
    DGVOrders.DataSource = BindingSource1
    DGVOrders.ClearSelection()
    con.Close()
    DGVOrders.Columns(0).Visible = False
    DGVOrders.Columns(1).HeaderText = "Order No"
    DGVOrders.Columns(2).HeaderText = "Cust Code"
    DGVOrders.Columns(3).HeaderText = "Name"
    DGVOrders.Columns(4).HeaderText = "Order Date"
    DGVOrders.Columns(5).HeaderText = "Order Price"
    DGVOrders.Columns(6).HeaderText = "Total Payment"
    DGVOrders.Columns(7).HeaderText = "Dues"
    For i = 0 To DGVOrders.RowCount - 1
        If DGVOrders.Rows(i).Cells(7).Value > 0 Then
            DGVOrders.Rows(i).Cells(7).Style.ForeColor = System.Drawing.Color.Red
        Else
            DGVOrders.Rows(i).Cells(7).Style.ForeColor = System.Drawing.Color.Green
        End If
     Next
    ' CHECK BOX
    Dim colmnchk As New DataGridViewCheckBoxColumn
    colmnchk.DataPropertyName = "chkSelect"
    colmnchk.HeaderText = "SELECT"
    colmnchk.Name = "chkSelect"
    DGVOrders.Columns.Add(colmnchk)
    For i = 0 To DGVOrders.RowCount - 1

    Next
    'CHECK BOX END
End Sub

Private Sub DGVOrders_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVOrders.CellValueChanged
        If DGVOrders.Columns(e.ColumnIndex).Name = "chkSelect" Then
            Dim checkCell As DataGridViewCheckBoxCell = _
           CType(DGVOrders.Rows(e.RowIndex).Cells("chkSelect"),  _
           DataGridViewCheckBoxCell)
            If checkCell.Value = True Then
               PaymentTable.Rows.Add(DGVOrders.Rows(e.RowIndex).Cells(0).Value, DGVOrders.Rows(e.RowIndex).Cells(7).Value)

         Else

            Dim toRemoveID As Integer = DGVOrders.Rows(e.RowIndex).Cells(0).Value
            For i = 0 To PaymentTable.Rows.Count - 1
                If PaymentTable.Rows(i).Item(0) = toRemoveID Then
                    PaymentTable.Rows(i).Delete()

                    Exit Sub
                End If
            Next
        End If
        DataGridView1.DataSource = PaymentTable
    End If
End Sub

可以用sombody来解决问题,或者如果我的代码错了,还有其他好的方法吗?

1 个答案:

答案 0 :(得分:0)

尝试使用其他活动,例如CellContentClick并测试ColumnIndex。这样做的缺点是,只要您点击任何单元格,事件就会触发。

  Private Sub DGVOrders_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGVOrders.CellContentClick
    'Only interested in the CheckBox column
    If e.ColumnIndex = colmnchk.Index Then
      Debug.WriteLine("In DGVOrders_CellContentClick for the checkbox")
    End If
  End Sub