Reputation: 559
I have a form with a DataGridView
and CheckBox
. What I want is when DataGridView
index changes it should change CheckBox
checked state, or if I press arrow down or up CheckBox
should change depending on cell value. Here is my code:
Public Sub cellclick()
Dim row As DataGridViewRow
Dim r As Integer = usergrid.CurrentRow.Index
row = Me.usergrid.Rows(r)
'the value of the cell("ACCES1") maybe yes or no
If row.Cells("ACCESS1").Value.ToString = "YES" Then
CheckBox1.CheckState = CheckState.Checked
'i also try checkbox1.checked=true
ElseIf row.Cells("ACCESS1").Value.ToString = "NO" Then
CheckBox1.CheckState = CheckState.Unchecked
'i also try checkbox1.checked=false
End If
End Sub
Private Sub usergrid_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles usergrid.CellEnter
cellclick()
End Sub
UPATE:
I change my if else code to this following lines of code but still not working heres my new code:
If row.Cells("ACCESS1").Value = "YES" Then
CheckBox1.Checked = True
MsgBox(row.Cells("ACCESS1").Value) 'this is working and it echoes me the cell content everytime i click or change the grid index. it message me to "YES" and sometimes "NO" depends upon on the cell content/value
Else
CheckBox1.Checked = False
MsgBox(row.Cells("ACCESS1").Value) 'this is working and it echoes me the cell content everytime i click or change the grid index
End If
Upvotes: 2
Views: 13669
Reputation:
I think you are not getting the current row. since your function is not focusing on it. so make the following changes in the method as well as call:
Private Sub usergrid_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles usergrid.CellEnter
cellclick(e)
End Sub
Public Sub cellclick(ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
Dim row As DataGridViewRow
row = Me.usergrid.Rows(e.RowIndex)
If row.Cells("ACCESS1").Value.ToString = "YES" Then
CheckBox1.CheckState = CheckState.Checked
ElseIf row.Cells("ACCESS1").Value.ToString = "NO" Then
CheckBox1.CheckState = CheckState.Unchecked
End If
End Sub
Upvotes: 0