簡體   English   中英

無法刪除 vb.net 中 datagridview 中的行

[英]Cannot remove rows in datagridview in vb.net

我的 datagridview 綁定到數據表。 我想在不影響數據表的情況下過濾 datagridview。 我知道一種方法是使用綁定源,但綁定源將在其過濾器中使用列名。 但我想讓用戶在文本框中輸入任何字符串進行過濾。 所以我通過引用 function PassFilter 來傳遞 datagridview,它刪除了 datagridview 中不需要的行。 我可以看到它確實在 datagridview 中執行了行刪除。 但是問題是datagridview在form上顯示的時候是沒有變化的,和數據表一樣。 我不明白為什么在 datagridview 上沒有刪除行

代碼如下:

DataGridView1.DataSource = table

PassFilter(DataGridView1, m_FilterQuick.strFilter.ToLower())
Private Sub PassFilter(ByRef datagridview As DataGridView, ByVal strFilter As String)
    Dim i As Integer = 0
    Dim j As Integer = 0
    Dim containStr As Boolean = False

    While i < datagridview.Rows.Count
        j = 0
        containStr = False
        'If all cells in a row does not contain strFilter, delete this row from datagridview
        While j < datagridview.Rows(i).Cells.Count
            Dim c As DataGridViewCell = datagridview.Rows(i).Cells(j)
            If Not c.Value Is DBNull.Value Or Nothing Then
                If c.Value.ToString().ToLower().Contains(strFilter) Then
                    containStr = True
                    Exit While
                End If

            End If
            j = j + 1
        End While
        If Not containStr Then
            datagridview.Rows.RemoveAt(i)
        End If
        i = i + 1
    End While

End Sub

您的大問題是您在遍歷該集合時刪除了 DataGridViewRowCollection 中的項目。 即使您沒有收到“索引超出范圍”錯誤,您最終也會刪除錯誤的行,因為當您刪除較早的行時,后面的行的索引值會發生變化。

您應該做的不是刪除行循環中的行,而是將行的索引添加到List(Of Integers)

完成對 DataGridView 中的所有行的迭代后, Reverse()列表並使用List中的行索引值通過RemoveAt()方法刪除行。

這就是我的意思:

Private Sub PassFilter(ByRef datagridview As DataGridView, ByVal strFilter As String)
    Dim i As Integer = 0
    Dim j As Integer = 0
    Dim containStr As Boolean = False
    ' Row indexes we'll remove later on.
    Dim deleteIndexList As List(Of Integer) = New List(Of Integer)

    While i < datagridview.Rows.Count
        j = 0
        containStr = False
        'If all cells in a row does not contain strFilter, delete this row from datagridview 
        While j < datagridview.Rows(i).Cells.Count
            Dim c As DataGridViewCell = datagridview.Rows(i).Cells(j)
            ' Note: you'll want to enclose the tests for DBNull.Value and Nothing in parens like I show here.
            If Not (c.Value Is DBNull.Value And c.Value Is Nothing) Then
                If c.Value.ToString().ToLower().Contains(strFilter) Then
                    containStr = True
                    Exit While
                End If

            End If
            j = j + 1
        End While
        If Not containStr Then
            ' Don't remove rows here or your row indexes will get out of whack!
            ' datagridview.Rows.RemoveAt(i)
            deleteIndexList.Add(i)
        End If
        i = i + 1
    End While

    ' Remove rows by reversed row index order (highest removed first) to keep the indexes in whack.
    deleteIndexList.Reverse()
    For Each idx As Integer In deleteIndexList
        datagridview.Rows.RemoveAt(idx)
    Next
End Sub

您是將數據顯示為只讀還是用於編輯?

如果您允許用戶進行編輯,您能否找到某種方法來隱藏您不想顯示的行,而不是嘗試刪除它們?

如果您將其顯示為只讀,則可以在過濾后獲取數據,並將其放入新的 datagridview ....(丑陋,是的,但可能的選項)

哦等等...如果您通過值而不是引用傳遞 datagridview,那么您在刪除時的更改將保持不變?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM