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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31778286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:22:04  来源:igfitidea点击:

How to change CheckBox.Checked in VB.NET based on "yes"/"no" column

vb.netcheckboxdatagridviewstring-comparison

提问by pretty me

I have a form with a DataGridViewand CheckBox. What I want is when DataGridViewindex changes it should change CheckBoxchecked state, or if I press arrow down or up CheckBoxshould change depending on cell value. Here is my code:

我有一个带有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

UPATE:

更新:

I change my if else code to this following lines of code but still not working heres my new code:

我将 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

回答by pretty me

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