c# datagridview filter rows without datasource code example

Example: 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);
}