如果Total列中的数据为零,如何从DataGridView中删除行

时间:2016-07-08 08:18:55

标签: vb.net datagridview

大家好,下午好。

我有一个程序可以将excel导入到datagridview中,但是在我导入之后看起来就是这样。

Link of the Image

正如您在上图中看到的那样,我有一个列Total,它由NULL(空字符串),0和某个数据组成。

我想知道如果列Total的数据为NULL0,我该如何删除整行。

我正在使用此代码,它适用于Datagridview

Private Sub step_3_delete_Zero()
    Dim SkipRemove As Boolean
    Dim Rowindex As Integer

    For Rowindex = DataGridView1.Rows.Count - 1 To 0 Step -1
        SkipRemove = False

        For Each Cell As DataGridViewCell In DataGridView1.Rows(Rowindex).Cells
            If Not Cell.Value.ToString = "0" Then
                SkipRemove = True
                Exit For
            End If
        Next

        If Not SkipRemove = False Then
            DataGridView1.Rows.RemoveAt(Rowindex)
        End If
    Next

End Sub

我希望有人帮助我,TY提前

1 个答案:

答案 0 :(得分:0)

在你的每个循环中,你说过要看整个单元格吗?

您是否正在查看该行中的每个单元格?

If SkipRemove = True Then还不比If Not SkipRemove = False Then更容易阅读吗?

我认为它不起作用,因为你循环并检查每个单元格而不是按行循环然后检查每一行上的特定单元格。

这样做会更容易吗?

For Each row As DataGridViewRow In DataGridView1.Rows
    If row.Cells.Item("Total").Value = "0" Then
             DataGridView1.Rows.Remove(row)
    End If
Next