在 VB.Net 中捕获 DataGridView CheckBox 的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3439777/
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
Capturing value of DataGridView CheckBox in VB.Net
提问by Furqan Sehgal
I have a datagridview (unbound). Fields are Name, Family Name and Phone No and a checkbox colum.
我有一个数据网格视图(未绑定)。字段是姓名、姓氏和电话号码以及复选框列。
There are ten rows in that DataGridView.
该 DataGridView 中有十行。
There is an OK button
有一个确定按钮
I need to get message of showing which rows user has checked. The message should appear when user clicks on the OK button. There could be several messages, checking each row one by one, in a loop.
我需要获取显示用户已检查哪些行的消息。当用户单击“确定”按钮时,应显示该消息。可能有几条消息,在循环中逐行检查。
I am not able to get this message. I tried following code in OK button :
我无法收到此消息。我在确定按钮中尝试了以下代码:
Dim strCB As String = dgvChooseQs.Rows(0).Cells(3).Value.ToString
Cell(3) is my checkbox. Do not consider Rows(0), at the moment I am just checking value at row 0
Cell(3) 是我的复选框。不要考虑 Rows(0),目前我只是在检查第 0 行的值
Thanks for your help.
谢谢你的帮助。
Furqan
富尔坎
回答by David Brunelle
Do not use the cell index. Your checkbox column must have a name so you should use it.
不要使用单元格索引。您的复选框列必须有一个名称,因此您应该使用它。
Otherwise, what you want to do would be something like this
否则,你想要做的就是这样
For each oRow as DataGridViewRow in dgvChooseQs.Rows If oRow.Cells("ColNamE").Value = True then 'do whatever you need to do. End if Next
If you feel you need to cast the column, then you can use CType, but the type is DataGridViewCheckBoxCell, not CheckBox.
如果觉得需要对列进行强制转换,那么可以使用CType,但类型是DataGridViewCheckBoxCell,而不是CheckBox。
回答by husam
You may cast the cell value to Boolean, and then check it, as follows:
您可以将单元格值转换为布尔值,然后进行检查,如下所示:
Dim RowIndex As Integer = ...
Dim ColumnIndex As Integer = ...
Dim IsTicked As Boolean = CBool(DataGridView1.Rows(RowIndex).Cells(ColumnIndex).Value)
If IsTicked Then
MessageBox.Show("You ticked the box.")
Else
MessageBox.Show("You cleared the box.")
End If
回答by aape
You need to do something like this:
你需要做这样的事情:
if ctype(dgvChooseQs.Rows(0).findcontrol("whateverYourCheckBoxIsNamed"), checkbox).checked then
'throw the message
end if
回答by Mauricio de León
I found a simple solution.
我找到了一个简单的解决方案。
Just change the cell focus after click on cell.
单击单元格后只需更改单元格焦点。
Private Sub DGV_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGV.CellContentClick
If e.ColumnIndex = "Here checkbox column id or name" Then
DGV.Item(e.ColumnIndex, e.RowIndex + 1).Selected = True
'Here your code
MsgBox DGV.Item(e.ColumnIndex, e.RowIndex).Value
End If
End Sub
Don't forget to check if the column of your (ckeckbox + 1) index exist.
不要忘记检查您的 (ckeckbox + 1) 索引的列是否存在。
回答by dinmohammadm
Set type of grid in dataGridView to 'DataGridViewCheckBoxXColumn' instead of DataGridViewCheckBoxColumn.
将 dataGridView 中的网格类型设置为“DataGridViewCheckBoxXColumn”而不是 DataGridViewCheckBoxColumn。
all problems will be solved
所有的问题都会得到解决
回答by Amazigh.Ca
The post is old but it can help in need: You have these three properties after casting your cell:
这篇文章很旧,但它可以帮助需要: 投射你的细胞后,你有这三个属性:
Private Sub YourDataGridView_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles YourDataGridView.CellMouseClick
Dim b, b1, b2 As Boolean
b = DirectCast(YourDataGridView.CurrentCell, DataGridViewCheckBoxCell).EditingCellValueChanged
b1 = CBool(DirectCast(YourDataGridView.CurrentCell, DataGridViewCheckBoxCell).EditingCellFormattedValue)
b2 = CBool(DirectCast(YourDataGridView.CurrentCell, DataGridViewCheckBoxCell).EditedFormattedValue)
End Sub