Question

I have a datagridview in which I populate via a loop and then use the cell painting went to make all cells that have te value " $0.00". What I'm trying to do now is after my populate loop executes I want to go through each row and if each cell contains " $0.00" I want to delete the entire row (row header included). How could I do this with a loop? I was looking around on the internet and came across the "Datagridview1.rows.remove(datagridiew1[row])". Could that be implemented to help me accomplish this? If so, how? Example code would be appreciated. Thank you!

*Edited to include code*

I have the two Subs in which I call the check row right after the populate datagridview sub

    Sub PopulateDataGridView()
    pb.Value = 0
    pb.Visible = True
    pb.Enabled = True
    'Loop through each column
    Dim cIndex As Integer = 0
    Dim rIndex As Integer = 0
    While cIndex < DataGridView1.ColumnCount



        'Loop through and populate each row in column
        rIndex = 0
        While rIndex < DataGridView1.RowCount

            'pb.Value = pb.Value + 1

            If cIndex = 0 Then
                'Set row header titles
                DataGridView1.Rows.Item(rIndex).HeaderCell.Value = sheet.Range("A1").Offset(rIndex + 1, cIndex).Value()

                DataGridView1.Rows(rIndex).Cells(cIndex).Value = sheet.Range("A1").Offset(rIndex + 1, cIndex + 1).Value()
            End If
            If cIndex > 0 Then
                DataGridView1.Rows(rIndex).Cells(cIndex).Value = sheet.Range("A1").Offset(rIndex + 1, cIndex + 1).Value()
            End If

            'Set column header title
            DataGridView1.Columns(cIndex).HeaderText = sheet.Range("A1").Offset(0, cIndex + 1).Value

            'Change last cell (Result) color Red or Green to represent positive gain or negative loss
            If rIndex = RowCount - 2 Then
                If DataGridView1.Rows(rIndex).Cells(cIndex).Value < 0 Then
                    DataGridView1.Item(cIndex, rIndex).Style.BackColor = Color.Red
                    DataGridView1.Item(cIndex, rIndex).Style.ForeColor = Color.White
                End If
                If DataGridView1.Rows(rIndex).Cells(cIndex).Value > 0 Then
                    DataGridView1.Item(cIndex, rIndex).Style.BackColor = Color.Green
                    DataGridView1.Item(cIndex, rIndex).Style.ForeColor = Color.White
                End If
                If DataGridView1.Rows(rIndex).Cells(cIndex).Value = 0 Then
                    DataGridView1.Rows(rIndex).Cells(cIndex).Value = "Broke Even"
                End If

            End If

            pb.Value = pb.Value + 1
            rIndex = rIndex + 1
        End While

        'Make column unsortable
        DataGridView1.Columns(cIndex).SortMode = DataGridViewColumnSortMode.NotSortable

        cIndex = cIndex + 1

    End While
    pb.Visible = False
    pb.Value = 0
    pb.Enabled = False

    DataGridView1.AutoResizeColumns()

    'Resize all Row Headers so user can see Row Titles without resizing
    DataGridView1.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)
End Sub

Sub EmptyRowCheck()
    Dim SkipRemove As Boolean
    'loop through rows in datagrid
    For Each Row As DataGridViewRow In DataGridView1.Rows
        SkipRemove = False
        'loop through each cell in row
        For Each Cell As DataGridViewCell In Row.Cells
            'if value is not $0.00 then set boolean and exit inner loop
            If Not Cell.Value = " $0.00" Then
                SkipRemove = True
                Exit For
            End If
        Next
        'check if to remove the row or not
        If Not SkipRemove = True Then
            DataGridView1.Rows.Remove(Row)
        End If
    Next
End Sub

My code would include

    PopulateDataGridView()
    EmptyRowCheck()

The issue I'm running into now is with this method it's skipping over every other empty row, only deleting half of the empty rows.

Was it helpful?

Solution

something like this could work:

    Dim SkipRemove As Boolean
    Dim Rowindex As Integer
    'loop through rows in datagrid starting from the bottom
    For Rowindex = DataGridView1.Rows.Count - 1 To 0 Step -1
        SkipRemove = False
        'loop through each cell in row
        For Each Cell As DataGridViewCell In DataGridView1.Rows(Rowindex).Cells
            If Not Cell.Value = "£0.00" Then
                SkipRemove = True
                Exit For
            End If
        Next
        'check if to remove the row or not
        If Not SkipRemove = True Then
            DataGridView1.Rows.RemoveAt(Rowindex)
        End If
    Next

this starts at the bottom of the datagridview and works up preventing the skipping issue.

OTHER TIPS

It should look something like this:

Dim RemoveThis AS Boolean = True

FOR EACH dgvrow AS Datagridviewrow IN Datagridview1.Rows
'Loop thru each row

    FOR i AS Integer = 0 to Datagridview1.Columncount
    'Loop thru each column/field of the current row

        IF NOT dgvrow(i)="$0.00" THEN RemoveThis = False
        'If any one of the cells does not contain the value "$0.00", do not remove the row
    NEXT

    If RemoveThis THEN Datagridview1.Rows.Remove(dgvrow)
    RemoveThis = True
NEXT
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top