LabRat
LabRat

Reputation: 2014

Hide rows if column contains particular text/ string

I have a datagridview wich I have no bound datasource too I also have a button and three rows in my datagridview. If my column named STATUS contains the word CLOSED I would like to hide that entire row but i dont want to delete it just hide it.

If anyone woyuld like to know I am ussing VB.net

How can I do this?

Upvotes: 1

Views: 2758

Answers (1)

Sean Wessell
Sean Wessell

Reputation: 3510

If you are using a bound datasource you want to capture the DataGridView.DataSourceChanged event.

Would look like this.

Private Sub DataGridView1_DataSourceChanged(sender As Object, e As System.EventArgs) Handles DataGridView1.DataSourceChanged

    For Each row As DataGridViewRow In DirectCast(sender, DataGridView).Rows

        If row.Cells("status").Value.ToString.ToLower.Contains("Closed") Then

            row.Visible = False

        End If

    Next


End Sub

If you are not using a datasource you would want to capture the DataGridView.RowsAdded event.

Would look like this.

Private Sub DataGridView1_RowsAdded(sender As Object, e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded

    Dim dg As DataGridView = sender

    If dg.Columns.Count > 0 And e.RowIndex <> 0 Then

        Dim theRow As DataGridViewRow = dg.Rows(e.RowIndex)

        If theRow.Cells("status").Value.ToString.ToLower.Contains("closed") Then

            theRow.Visible = False

        End If

    End If

End Sub

Upvotes: 1

Related Questions