[英]Vb.net Change the back color of picturebox whose corresponding checkbox.checked = true
[英]How to change CheckBox.Checked in VB.NET based on “yes”/“no” column
我有一個帶有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
我認為你沒有得到當前行。 因為你的功能沒有專注於它。 因此在方法中進行以下更改並調用:
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.