Answers for "Search or Filter Data in GridView c#"

PHP
0

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);
}
Posted by: Guest on October-25-2020

Browse Popular Code Answers by Language