vb.net 在Datagridview VB.NET中选择行时检查复选框列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37247705/
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
Make Checkbox Column Checked when Row Selected in Datagridview VB.NET
提问by Beldi Anouar
Hello Everyone Good Afternoon.
大家好,下午好。
I have a question,
我有个问题,
I have Checkbox Column in Datagridview and the Column is in 0.
我在 Datagridview 中有复选框列,列在 0 中。
How can i make the Checkbox Column Checked when a Specific Row is Selected in Datagridview? Make the checkbox column checked when a row is Selected.
在 Datagridview 中选择特定行时,如何检查复选框列?选中一行时选中复选框列。
Here is my code
这是我的代码
Code connected to another Code when populating Data from Database to Datagridview
将数据从数据库填充到 Datagridview 时,代码连接到另一个代码
Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
checkBoxColumn.HeaderText = "Tag"
checkBoxColumn.Width = 30
checkBoxColumn.Name = "checkBoxColumn"
DataGridView1.Columns.Insert(0, checkBoxColumn)
Code when selecting row.
选择行时的代码。
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If e.ColumnIndex = DataGridView1.Columns(0).Index Then
DataGridViewCheckBoxColumn_Uncheck()
Dim cell As DataGridViewCheckBoxCell = DataGridView1.Rows(e.RowIndex).Cells(0)
cell.Value = cell.TrueValue
End If
End Sub
Private Sub DataGridViewCheckBoxColumn_Uncheck()
For Each row As DataGridViewRow In DataGridView1.Rows
Dim cell As DataGridViewCheckBoxCell = row.Cells(0)
cell.Value = cell.FalseValue
Next
End Sub
My code has no error but the prob. here is I really need to Select the Checkbox Column and Checked on it and when the Row Selection Changed the Last Selected is Unchecked.
我的代码没有错误,但问题。这是我真的需要选择复选框列并选中它,当行选择更改时,最后选择的是未选中的。
I hope you get me.
我希望你能得到我。
TYSM for future Help
TYSM 未来帮助
回答by Beldi Anouar
Try to change your code to :
尝试将您的代码更改为:
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim cell As DataGridViewCheckBoxCell = DataGridView1.Rows(e.RowIndex).Cells(0)
DataGridViewCheckBoxColumn_Uncheck()
cell.Value = True
End Sub
Private Sub DataGridViewCheckBoxColumn_Uncheck()
For Each row As DataGridViewRow In DataGridView1.Rows
Dim cell As DataGridViewCheckBoxCell = row.Cells(0)
cell.Value = False
Next
End Sub

