【发布时间】:2022-03-22 12:08:45
【问题描述】:
我有一个带有DataGridView 和CheckBox 的表单。我想要的是当DataGridView 索引更改时它应该更改CheckBox 选中状态,或者如果我按下箭头向下或向上CheckBox 应该根据单元格值更改。这是我的代码:
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
更新:
我将 if else 代码更改为以下代码行,但仍然无法正常工作,这是我的新代码:
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
【问题讨论】:
-
access1的可能值为'yes' or 'no' -
调试你的代码。将断点放在
cellclick函数中。好像没有调用你的函数,CellEnter事件没有触发 -
CellEnter与当前行更改索引之间存在差异。哪一个?还有你在说什么上下箭头? -
您可以查看:RowEnter 或 RowLeave 事件,因为这会触发索引更改...从那里执行您需要的操作。
-
@436f6465786572
cellenter可能,但它在cellcontentclick and cellenter event中仅适用于 msgbox 行(问题已更新)。我的意思是它在键盘上按上下箭头..
标签: vb.net checkbox datagridview string-comparison