Hi ernestpauld,
There are two ways to hide the DataGridView rows.
1. In the select query apply where condition to fetch the online records only.
SELECT * FROM Accounts WHERE AccountStatus = 'offline'
2. Loop through the DataGridView rows and check row with if condition. If status is offline then hide the row.
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.AllowUserToAddRows = false;
using (DataTable dt = new DataTable())
{
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Name"), new DataColumn("AccountStatus") });
dt.Rows.Add("John Hammond", "Offline");
dt.Rows.Add("Mudassar Khan", "Online");
dt.Rows.Add("Suzanne Mathews", "Offline");
dt.Rows.Add("Robert Schidner", "Online");
dataGridView1.DataSource = dt;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells["AccountStatus"].Value.ToString().ToLower() == "offline")
{
CurrencyManager currencyManager = (CurrencyManager)BindingContext[dataGridView1.DataSource];
currencyManager.SuspendBinding();
dataGridView1.CurrentCell = null;
row.Visible = false;
}
}
}
}
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dataGridView1.AllowUserToAddRows = False
Using dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Name"), New DataColumn("AccountStatus")})
dt.Rows.Add("John Hammond", "Offline")
dt.Rows.Add("Mudassar Khan", "Online")
dt.Rows.Add("Suzanne Mathews", "Offline")
dt.Rows.Add("Robert Schidner", "Online")
dataGridView1.DataSource = dt
For Each row As DataGridViewRow In dataGridView1.Rows
If row.Cells("AccountStatus").Value.ToString().ToLower() = "offline" Then
Dim currencyManager As CurrencyManager = CType(BindingContext(dataGridView1.DataSource), CurrencyManager)
currencyManager.SuspendBinding()
dataGridView1.CurrentCell = Nothing
row.Visible = False
End If
Next
End Using
End Sub
Screenshot