Paul Edward Pagente Paul Edward Pagente - 2 months ago 28
Vb.net Question

Show MessageBox on Datagridview Checkbox before Check or UnCheck

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

Answer

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.