phapha pha phapha pha - 9 days ago 7
C# Question

How to filter Integer in DataView RowFilter using combobox and textbox?

So, I have a comoboBox1 and textBox1

comboBox1 is set to the name of columns from dataview dv

and the textBox1 is for the search

So, I tried simple code something like this :

dv.RowFilter = "Convert([City Number], System.String) LIKE '%2%'";


and the code above works well

but then I tried to replace City Number and %2% with value from comboBox1 and textBox1,

into something like this.

dv.RowFilter = "Convert([comboBox1.SelectedItem.ToString()], System.String) LIKE '%{0}%'", textBox1.Text;


The syntax seems wrong, but you know what I mean.

So how to fix that?

Answer

You can use string.Format to make the filter expression this way:

var column = comboBox1.GetItemText(comboBox1.SelectedItem);
var value = textBox1.Text;
var filter = string.Format("Convert([{0}], System.String) LIKE '%{1}%'", column, value);
dv.RowFilter = filter;