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

扶醉桌前 提交于 2021-01-29 01:58:37

问题


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?


回答1:


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;


来源:https://stackoverflow.com/questions/40893932/how-to-filter-integer-in-dataview-rowfilter-using-combobox-and-textbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!