简体   繁体   English

Datagridview不会更改第一行的复选框(vb.net)

[英]Datagridview won't change checkbox of first row (vb.net)

I have a datagridview (here called cameraTable). 我有一个datagridview(这里称为cameraTable)。 One column of the datagridview is purely checkboxes. datagridview的一列仅是复选框。 I have a button that when clicked will check all the boxes in the column, aka select all. 我有一个按钮,单击该按钮将选中列中的所有框,也就是全选。 Here's the code: 这是代码:

Private Sub btnSelectAll_Click(sender As Object, e As EventArgs) Handles btnSelectAll.Click

    For Each row As DataGridViewRow In cameraTable.Rows
        DirectCast(row.Cells("CheckBox"), DataGridViewCheckBoxCell).Value = _
            True
    Next
End Sub

The problem I'm having is that the initial box (ie row zero) never changes to checked. 我遇到的问题是,初始框(即第零行)永远不会更改为选中状态。 I tried these: 我尝试了这些:

DirectCast(cameraTable(0, 0), DataGridViewCheckBoxCell).Value = True ' doesn't work 
cameraTable.Rows(0).Cells(0).Value = True ' doesn't work 

Neither change the box to checked, but the other rows within the column change without a problem. 都不会将复选框更改为选中状态,但是该列中的其他行不会出现问题。 I even did a msgbox that prints out the value, and turns out to be "True". 我什至做过一个msgbox,可以打印出该值,结果是“ True”。 So the value of the checkbox has changed, but the check box in the datagridview is still unchecked. 因此,复选框的值已更改,但datagridview中的复选框仍未选中。 Any ideas? 有任何想法吗?

Thanks for all the help! 感谢您的所有帮助!

-K -K

The datagrid will not get INotifyPropertyChanged events (or will not process them) for the row/cell that is currently selected. 对于当前选定的行/单元,该数据网格将不会获得INotifyPropertyChanged事件(或将不会处理它们)。 If you have other columns that can be edited, move the focused column to it. 如果您还有其他可编辑的列,请将焦点所在的列移至该列。

If all else fails, reload the grid after seting the values by setting the datasource = nothing and then back to what it was. 如果所有其他方法均失败,请在设置值之后通过设置数据源= none来重新加载网格,然后返回到原来的状态。

Steve is right. 史蒂夫是对的。 What I did was when the check box is clicked, I set the focus to another cell on that row (my dgv is set to FullRowSelect): 我所做的是,当单击复选框时,将焦点设置到该行的另一个单元格(我的dgv设置为FullRowSelect):

Private Sub dgv_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellContentClick
  If e.ColumnIndex = iCBColIDX Then
    dgv.CurrentCell = dgv.Rows(e.RowIndex).Cells("sts")
  End If
End Sub

Make your checkbox column width=20 instead of 18. 使您的复选框列的宽度为20,而不是18。

No, I don't know why, but it helps. 不,我不知道为什么,但是有帮助。

Thanks this man . 谢谢这个人

PS. PS。 It isn't a joke, it's a bug. 这不是在开玩笑,而是一个错误。

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

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