سؤال

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

هل كانت مفيدة؟

المحلول

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;

نصائح أخرى

Try this:

var dv = new DataView(table);

dv.RowFilter = String.IsNullOrEmpty(vendorTextBox.Text)
             ? ""
             : String.Format("vendor like '%{0}%' and (model like '%{1}%' or '{1}' = '')", vendorTextBox.Text, modelTextBox.Text);

purchase_mobile_DG.DataSource = dv;

Also, please name your controls "vendorTextBox" and "modelTextBox"

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top