简体   繁体   English

在 VB.Net 中捕获 DataGridView CheckBox 的值

[英]Capturing value of DataGridView CheckBox in VB.Net

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. Cell(3) 是我的复选框。 Do not consider Rows(0), at the moment I am just checking value at row 0不要考虑行(0),目前我只是检查第 0 行的值

Thanks for your help.谢谢你的帮助。

Furqan富尔坎

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。

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

You need to do something like this:你需要做这样的事情:

if ctype(dgvChooseQs.Rows(0).findcontrol("whateverYourCheckBoxIsNamed"), checkbox).checked then 

'throw the message

end if

Only the third example worked for me but I had to add a Timer只有第三个示例对我有用,但我必须添加一个 Timer

Private Sub DgvElencoFile_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DgvElencoFile.CellClick

    'Variabili gestione programma
    Dim numerocolonnaSelezionata As Integer
    Dim numeroColonnaSelezionataPerDowonload As Integer
    Dim numeroRigaSelezionata As Integer

    numeroRigaSelezionata = e.RowIndex
    NumerocolonnaSelezionata = e.ColumnIndex

    If NumeroRigaSelezionata < 0 Then
        ClaFunzSgnSon.SegnaleSonoro(ClaFunzSgnSon.EnTipoSgnSon.ErroreImpostazioneDati)
        GoTo FineSubFunz
    End If
    numeroColonnaSelezionataPerDowonload = -1

    If DgvElencoFile.Rows(numeroRigaSelezionata).Cells(ClnDownLoad.Name).Selected Then numeroColonnaSelezionataPerDowonload = numerocolonnaSelezionata

    If numeroColonnaSelezionataPerDowonload >= 0 Then
        TimVisCheckDownLoad.Start()
    End If

FineSubFunz: End Sub FineSubFunz:结束子

Private Sub TimVisCheckDownLoad_Tick(sender As Object, e As EventArgs) Handles TimVisCheckDownLoad.Tick TimVisCheckDownLoad.Stop() Private Sub TimVisCheckDownLoad_Tick(sender As Object, e As EventArgs) 处理 TimVisCheckDownLoad.Tick TimVisCheckDownLoad.Stop()

    Dim isTickedOn = CBool(DirectCast(DgvElencoFile.CurrentCell, DataGridViewCheckBoxCell).EditingCellFormattedValue)

    If isTickedOn Then
        MessageBox.Show("You ticked the box.")
    Else
        MessageBox.Show("You cleared the box.")
    End If

End Sub

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) 索引的列是否存在。

Set type of grid in dataGridView to 'DataGridViewCheckBoxXColumn' instead of DataGridViewCheckBoxColumn.将 dataGridView 中的网格类型设置为“DataGridViewCheckBoxXColumn”而不是 DataGridViewCheckBoxColumn。

all problems will be solved所有的问题都会得到解决

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

it's a long time since this question was sent, but can be useful this answer to anybody with the same issue.自从发送此问题以来已经有很长时间了,但是对于遇到相同问题的任何人来说,这个答案都会很有用。 In my case, I used (I'm using your notation):就我而言,我使用(我正在使用您的符号):

dgvChooseQs.Item(6, vCont).State

where 6 is the checkbox column number, in my case column 6. vCont is an iteration counter in a FOR NEXT loop.其中 6 是复选框列号,在我的例子中是第 6 列。 vCont 是 FOR NEXT 循环中的迭代计数器。 If State equals 32, checkbox was checked, else State will be zero.如果 State 等于 32,则选中复选框,否则 State 将为零。 I hope this can be of help.我希望这能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM