在选中或取消选中之前在 Datagridview 复选框上显示 MessageBox

Show MessageBox on Datagridview Checkbox before Check or UnCheck

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

假设我在 Datagridview 中有一行带有复选框,它的值为 True(Checked),我将单击它。我怎样才能先显示这样的内容?

"Are you sure you want to uncheck this? Yes or No"

是 = 取消选中

否 = 还是一样(选中)

这是我的代码,其中包含我想要的输出,但它不起作用

 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

处理 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

您可以先这样设置 DataGridViewReadOnly property of column to True, then handle 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,您可以同时使用 code and designer


仅在取消选中单元格时得到确认:

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