如何根据“是”/“否”列在 VB.NET 中更改 CheckBox.Checked

问题描述 投票:0回答:1

我有一个带有

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
vb.net checkbox datagridview string-comparison
1个回答
0
投票

我认为你没有得到当前行。因为你的功能没有专注于它。所以在方法和调用中进行以下更改:

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 
© www.soinside.com 2019 - 2024. All rights reserved.