选中或取消选中前显示在DataGridView的复选框的MessageBox(Show Messag

2019-09-30 10:27发布

如何显示更新前一个MsgBox在DataGridView中的复选框?

比方说,我有在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

Answer 1:

您可以先设置ReadOnly列的属性为True ,那么处理CellContentClick事件DataGridView是这样的:

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 ,你可以同时使用的代码和设计 。


为了得到取消选中该单元格,只有当确认:

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


Answer 2:

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


文章来源: Show MessageBox on Datagridview Checkbox before Check or UnCheck