Search code examples
c#datagridviewdatagridviewcolumn

filter datagirdview on input of two text box


this is what i am trying ...:

DataView dv = new DataView(table);
dv.RowFilter = String.Format("model like '%{0}%'", textBox2.Text);
if (!String.IsNullOrEmpty(textBox1.Text))
{
   dv.RowFilter = String.Format("vendor like '%{0}%'", textBox1.Text);
}

purchase_mobile_DG.DataSource = dv;

where table is the datatable which have all the data from database ....i have two text box so what i want to do is that when i type in first text box it filter datagridview and after that when i write in second textbox it filters data from the filtered datagridview of first text box


Solution

  • you should use my edited code it as

    DataView dv = new DataView(table);
    if (!String.IsNullOrEmpty(textBox1.Text))
    {
       dv.RowFilter = String.Format("vendor like '%{0}%'", textBox1.Text);
    }
    
    dv.RowFilter = dv.RowFilter == "" ? String.Format("model like '%{0}%'", textBox2.Text) : dv.RowFilter + String.Format("AND model like '%{0}%'", textBox2.Text);
    
    purchase_mobile_DG.DataSource = dv;