【发布时间】:2021-11-16 11:25:51
【问题描述】:
我正在使用 VB.NET 中的表单
有一个带有复选框列的 DatagridView 表。
见下图:
我对这个问题感兴趣:如何在单击复选框时(当我们激活选中状态时)将行索引添加到列表中,并在取消选中复选框时将其从列表中删除?
尝试了以下方法,但这不是正确的解决方案:
If e.ColumnIndex = chk_column.Index Then
If e.RowIndex >= 0 Then
Try
For Each row As DataGridViewRow In dataGridNames.Rows
Dim cell As DataGridViewCheckBoxCell = TryCast(row.Cells(5), DataGridViewCheckBoxCell)
If cell.Value Is cell.FalseValue Then
bList_indexes.Add(DataGridnames.CurrentCell.RowIndex)
Exit For
Else 'If cell.Value Is cell.TrueValue Then
bList_indexes.RemoveAt(DataGridnames.CurrentCell.RowIndex)
End If
Next
Catch ex As Exception
'Show the exception's message.
'MessageBox.Show(ex.Message)
'Throw New Exception("Something happened.")
End try
End If
End If
【问题讨论】:
-
该代码毫无意义。该事件的全部意义在于它为您提供了受影响单元格的行索引和列索引,那么为什么要遍历网格中的所有行呢?您已经获得了行索引,因此您应该只关心该索引处的行。事实上,您甚至都不关心行,因为您的集合包含索引。只需将当前索引添加到列表中或将其从列表中删除。就是这样,就是这样。
标签: vb.net winforms datagridview