filter datagridview c# code example

Example 1: c# datagridview filter

DataTable dt = new DataTable();
DataView dv = new DataView(dt);
// Filter by an expression. Filter all rows where column 'Col' have values greater or equal than 3
dv.RowFilter = "Col < 3";

dataGridView1.DataSource = dv;

Example 2: Search or Filter Data in GridView c#

DataTable dtSales = new DataTable();
string filterField = "Country";

private void Form1_Load(object sender, EventArgs e)
{
   dtSales.Columns.Add("Country", typeof(string));
   dtSales.Columns.Add("Sales", typeof(int));

   dtSales.Rows.Add(new object[] { "Argentina", 2000 });
   dtSales.Rows.Add(new object[] { "Belgium", 4500 });
   ...

   dataGridView1.DataSource = dtSales;
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
   dtSales.DefaultView.RowFilter = string.Format("[{0}] LIKE '%{1}%'", filterField, textBox1.Text);
}