0

I have a label that calculates how many rows the datagridview has and certainly do not want a blank row to be included. It has a blank row at the very top of the datagridview and sometimes in the middle. If it is possible, I want to delete all empty rows.

This code is not working:

For i As Integer = 1 To DataGridView1.RowCount - 2

            If DataGridView1.Rows(i).Cells(0).Value.ToString() = "" OrElse DataGridView1.Rows(i).Cells(8).Value.ToString() = "" Then


                DataGridView1.Rows.RemoveAt(i)
                i -= 1

            End If
        Next

Edited by CodingSource

3
Contributors
5
Replies
28
Views
2 Years
Discussion Span
Last Post by zizo.ahmed.779
0

I used the following Sub Procedure called DeleteRow but still didn't work:

   Dim Empty As Boolean = True

    For i As Integer = 0 To DataGridView1.Rows.Count - 1
        Empty = True
        For j As Integer = 0 To DataGridView1.Columns.Count - 1
            If DataGridView1.Rows(i).Cells(j).Value IsNot Nothing AndAlso DataGridView1.Rows(i).Cells(j).Value.ToString() <> "" Then
                Empty = False
                Exit For
            End If
        Next
        If Empty Then
            DataGridView1.Rows.RemoveAt(i)
        End If
    Next

P.S. Is creating a Sub Procedure for that and pasting that Sub Procedure to Form's Load event is wise?

0

Your problem is in construction of For Loop.
Removing Loops must be ran from highest listitem count to lowest by steping -1. Whatever may be the object is a ListBox, ComboBox, DataGridView or ListView.
Always remember that, in for loop the upper limits must be fixed. You can not change through its execution.You can change/set Lower Limit through execution.
The codes should be

For i As Integer = DataGridView1.RowCount - 1 To 1 Step -1
    If Trim(DataGridView1.Rows(i).Cells(0).Value.ToString()) = "" Then
        DataGridView1.Rows.RemoveAt(i)

    End If
Next

Hope it can help you.

0
Dim blank As Boolean = True
        For Each _row As DataGridViewRow In dgv.Rows
            blank = True
            For i As Integer = 0 To _row.Cells.Count - 1
                If _row.Cells(i).Value IsNot Nothing AndAlso _row.Cells(i).Value <> "" Then
                    blank = False
                    Exit For
                End If
            Next
            If blank Then
                If Not _row.IsNewRow Then
                    dgv.Rows.Remove(_row)

                End If
            End If
        Next
0

this code working for me only in one datagridview bur what if i want to clear from multi datagridview???

sorry for posting 2 times but i couldn't write comment with code in the same reply

0

i used new code to select data >0
"select * from table where coloumn>0
coloumn is coloumn header

it worked for me for 5 datagridview at the same time and remove all null or empty rows ....try

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.