【问题标题】:VB.NET: Add\remove row indexes of datagridview checkboxes rowsVB.NET:添加\删除 datagridview 复选框行的行索引
【发布时间】: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


【解决方案1】:

使用 DataSources 可以让您从处理 DataGridView 事件中的逻辑中解脱出来。无论如何,您不应该在 UI 上执行 [太多] 业务逻辑。

这是我用来表示您的数据的类。

Public Class ClassWithSelect
    Public Property [Select] As Boolean
    Public Property Name As String
    Public Sub New(s As Boolean, n As String)
        Me.Select = s
        Me.Name = n
    End Sub
End Class

以及设置数据源的所有代码

Private myDataSource As List(Of ClassWithSelect)
Private selectedIndices As List(Of Integer)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    myDataSource = Enumerable.Range(65, 10).Select(Function(i) New ClassWithSelect(False, Chr(i).ToString())).ToList()
    DataGridView1.DataSource = myDataSource
    updateSelectedIndices()
End Sub

Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
    updateSelectedIndices()
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
End Sub

Private Sub updateSelectedIndices()
    selectedIndices = New List(Of Integer)()
    For i = 0 To myDataSource.Count - 1
        If myDataSource(i).Select Then selectedIndices.Add(i)
    Next
    ListBox1.DataSource = selectedIndices
End Sub

最后的结果

现在您无需访问 UI 即可获取索引以进行进一步处理,因为它们位于类级变量 selectedIndices 中。 UI 用于用户 I/O,而不是用于存储状态。

  • 注意:事件处理程序取自this answer,但this answer 也被链接为对检查更改处理程序的改进,但我觉得复杂性会分散我的回答。如果您发现需要快速点击,请查看后者。
  • 另请注意:方法 updateSelectedIndices() 内部应该有一个 InvokeRequired 检查,如果您打算在 UI 线程之外执行工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 2012-12-23
    • 2010-12-12
    • 2018-06-30
    相关资源
    最近更新 更多