簡體   English   中英

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

[英]How to change CheckBox.Checked in VB.NET based on “yes”/“no” column

我有一個帶有DataGridViewCheckBox的表單。 我想要的是當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

我認為你沒有得到當前行。 因為你的功能沒有專注於它。 因此在方法中進行以下更改並調用:

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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM