在Check或UnCheck之前,在Datagridview复选框上显示MessageBox [英] Show MessageBox on Datagridview Checkbox before Check or UnCheck

查看:195
本文介绍了在Check或UnCheck之前,在Datagridview复选框上显示MessageBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果在Datagridview中有一个复选框,那么它的值为True(已检查)我会点击它。如何首先显示这样的东西?



您确定要取消选中吗?是或否



是=取消选中



否=仍然相同(已选)



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

  Private Sub DataGridView3SelectAll_CurrentCellDirtyStateChanged(
ByVal sender As Object,
ByVal e作为EventArgs)处理DataGridView3.CurrentCellDirtyStateChanged

RemoveHandler DataGridView3.CurrentCellDirtyStateChanged,
AddressOf DataGridView3SelectAll_CurrentCellDirtyStateChanged

如果TypeOf DataGridView3.CurrentCell是DataGridViewCheckBoxCell然后
DataGridView3.EndEdit( )
Dim Checked As Boolean = CType(DataGridView3.CurrentCell.Value,Boolean)
Dim xx As String
xx = MsgBox(你确定要保存更改吗,vbYesNo)
如果xx = vbYesN o然后
如果Checked = True然后
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然后
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。
Dim cmdinsert As MySqlCommand = New MySqlCommand(update stock_issuance set&= 0其中`& s& `=`&和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


解决方案

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

  Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object,_ 
ByVal e As DataGridViewCellEventArgs )处理DataGridView1.CellContentClick
如果(e.ColumnIndex = 0 AndAlso e.RowIndex> = 0)然后
D im 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 可以同时使用< a href =https://msdn.microsoft.com/en-us/library/cze614bb(v=vs.110).aspx =nofollow>代码和设计师






只有取消选中单元格才能确认:

  Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object ,_ 
ByVal e As DataGridViewCellEventArgs)处理DataGridView1.CellContentClick
如果(e.ColumnIndex = 0 AndAlso e.RowIndex> = 0)然后
Dim值e = DirectCast(DataGridView1(e.ColumnIndex,e.RowIndex).FormattedValue,_
Nullable(Of Boolean))
如果(value.HasValue AndAlso value = True)然后
Dim result = MessageBox.Show(确定取消选中项目?,,_
MessageBoxButtons.YesNoCancel)
如果(result = System.Windows.Forms.DialogResult.Yes)然后
DataGridView1 (e.ColumnIndex,e.RowIndex).Value = False
End If
Else
DataGridView1(e.ColumnIndex,e.RowIndex).Value = True
End If
结束If
End Sub


How to show a msgbox before update the checkbox in datagridview?

Lets say I have a row with checkbox in Datagridview and the value of it is True(Checked) and I will click it. How can I show something like this first?

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

Yes = Uncheck

No = Still the same (Checked)

here is my code with the output i want but its not working

 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

解决方案

You can first set ReadOnly property of column to True, then handle CellContentClick event of DataGridView this way:

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

To make the column ReadOnly you can use both code and designer.


To get confirm only when unchecking the cell:

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

这篇关于在Check或UnCheck之前,在Datagridview复选框上显示MessageBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆