在Check或UnCheck之前在Datagridview复选框上显示MessageBox

时间:2016-09-16 10:24:20

标签: vb.net winforms checkbox datagridview datagridviewcheckboxcell

如何在更新datagridview中的复选框之前显示msgbox?

假设我在Datagridview中有一行带复选框,并且它的值为True(已选中),我将单击它。我怎么能首先展示这样的东西?

“您确定要取消选中此项吗?是或否”

是=取消选中

否=仍然相同(已检查)

这是我的代码,我想要的输出,但它无法正常工作

 Private Sub DataGridView3SelectAll_CurrentCellDirtyStateChanged(
      ByVal sender As Object,
      ByVal e As EventArgs) Handles DataGridView3.CurrentCellDirtyStateChanged

        RemoveHandler DataGridView3.CurrentCellDirtyStateChanged,
            AddressOf DataGridView3SelectAll_CurrentCellDirtyStateChanged

        If TypeOf DataGridView3.CurrentCell Is DataGridViewCheckBoxCell Then
            DataGridView3.EndEdit()
            Dim Checked As Boolean = CType(DataGridView3.CurrentCell.Value, Boolean)
            Dim xx As String
            xx = MsgBox("Are you sure you want to save changes?", vbYesNo)
            If xx = vbYesNo Then
                If Checked = True Then
                    Dim s As String = (DataGridView3.Columns(DataGridView3.CurrentCell.ColumnIndex).DataPropertyName)
                    Dim x As Integer
                    x = DataGridView3.CurrentCell.RowIndex
                    Dim con1 As MySqlConnection = New MySqlConnection("datasource=192.168.2.87;database=inventory;userid=root;password=admin1950")
                    Dim cmdinsert As MySqlCommand = New MySqlCommand("update stock_issuance set `" & s & "` = 1 where `" & s & "` = `" & s & "` and Month = '" & DataGridView3.Rows(x).Cells(1).Value & "'", con1)
                    con1.Open()
                    cmdinsert.ExecuteNonQuery()
                    con1.Close()
                ElseIf Checked = False Then
                    Dim s As String = (DataGridView3.Columns(DataGridView3.CurrentCell.ColumnIndex).DataPropertyName)
                    Dim x As Integer
                    x = DataGridView3.CurrentCell.RowIndex
                    Dim con1 As MySqlConnection = New MySqlConnection("datasource=192.168.2.87;database=inventory;userid=root;password=admin1950")
                    Dim cmdinsert As MySqlCommand = New MySqlCommand("update stock_issuance set `" & s & "` = 0 where `" & s & "` = `" & s & "` and Month = '" & DataGridView3.Rows(x).Cells(1).Value & "'", con1)
                    con1.Open()
                    cmdinsert.ExecuteNonQuery()
                    con1.Close()
                End If
            Else

            End If
        End If


        AddHandler DataGridView3.CurrentCellDirtyStateChanged,
            AddressOf DataGridView3SelectAll_CurrentCellDirtyStateChanged
    End Sub

2 个答案:

答案 0 :(得分:0)

处理CellValidating事件。然后,如果它是您的列,请弹出msgbox。如果他们希望中止更改,请设置e.Cancel=true

Private Sub dgv_CellValidating(sender As Object, e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgv.CellValidating
    If dgv.Columns(e.ColumnIndex) Is myCheckboxColumn Then
        If DialogResult.Yes <> MessageBox.Show("Are you sure", "?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) Then
            e.Cancel = True
        End If
    End If
End Sub

答案 1 :(得分:0)

您可以先将列的ReadOnly属性设置为True,然后以DataGridView这样处理CellContentClick事件:

Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, _
    ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    If (e.ColumnIndex = 0 AndAlso e.RowIndex >= 0) Then
        Dim result = MessageBox.Show("Check Item?", "", MessageBoxButtons.YesNoCancel)
        If (result = System.Windows.Forms.DialogResult.Yes) Then
            DataGridView1(e.ColumnIndex, e.RowIndex).Value = True
        Else
            DataGridView1(e.ColumnIndex, e.RowIndex).Value = False
        End If
    End If
End Sub

要制作专栏ReadOnly,您可以同时使用codedesigner

仅在取消选中单元格时才能确认:

Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, _
    ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    If (e.ColumnIndex = 0 AndAlso e.RowIndex >= 0) Then
        Dim value = DirectCast(DataGridView1(e.ColumnIndex, e.RowIndex).FormattedValue, _
                               Nullable(Of Boolean))
        If (value.HasValue AndAlso value = True) Then
            Dim result = MessageBox.Show("Are you sure to uncheck item?", "", _
                                          MessageBoxButtons.YesNoCancel)
            If (result = System.Windows.Forms.DialogResult.Yes) Then
                DataGridView1(e.ColumnIndex, e.RowIndex).Value = False
            End If
        Else
            DataGridView1(e.ColumnIndex, e.RowIndex).Value = True
        End If
    End If
End Sub