【问题标题】:Show MessageBox on Datagridview Checkbox before Check or UnCheck在选中或取消选中之前在 Datagridview 复选框上显示消息框
【发布时间】:2017-01-24 13:04:12
【问题描述】:

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

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

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

是 = 取消选中

否 = 还是一样(选中)

这是我想要的输出代码,但它不工作

 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

【问题讨论】:

    标签: vb.net winforms checkbox datagridview datagridviewcheckboxcell


    【解决方案1】:

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

    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
    

    【讨论】:

    • 如果您对此答案有任何疑问,请告诉我。
    • 我只能说你是我的主人 TYSSSSSMMM +1 并批准
    • 先生你还在吗?
    • 先生,为了更简单,我们可以做这样的事情吗? msgbox 将显示“您确定要保存更改吗?”如果是,则检查当前值。如果值为是,则将其设为否并将值保存在数据库中,如果否,则将其设为是并保存到数据库。如果我在消息框中单击“否”,则所选值保持不变。我希望我说得更清楚,先生。 :)
    • 我相信目前的帖子很清楚,您可以简单地在解决方案上应用任何其他标准:)
    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      相关资源
      最近更新 更多