如何使用RowFilter过滤GridView中的数字和日期? [英] How to filter number and date in GridView using RowFilter?
问题描述
我正在使用C#编写Web应用程序。我想通过从列表框中选择一列来过滤GridView,然后立即根据用户使用RowFilter输入到文本框中的文本过滤表格。
我几乎已经完成了整个部分的管理工作,但我一直试图解决数字和日期的比较。
DataTable dt = new DataTable();
dt = GridView1.DataSource as DataTable;
dt.DefaultView.RowFilter = string.Format(ListBox1.SelectedItem.Text +LIKE'%{0}%',textBox1.Text);
对于数字,我试过类似的东西(没有工作):
dt.DefaultView.RowFilter = string.Format(ListBox1.SelectedItem.Text +='#{0}#',textBox1.Text);
我猜数字和日期的比较与文本几乎相同?
我认为以下内容对您有所帮助...
string colname = ListBox1.SelectedItem.Text;
字符串值= textBox1.Text;如果(colname!= null&& dt.Columns [colname]!= null)
if(Byte,Decimal,Double,Int16,Int32,Int64,SByte, Single,UInt16,UInt32,UInt64,。Contains(dt.Columns [colname] .DataType.Name +,))
{
dv.RowFilter = colname +=+ value;
}
else if(dt.Columns [colname] .DataType == typeof(string))
{
dv.RowFilter = string.Format(colname +LIKE'% {0}%',value);
}
else if(dt.Columns [colname] .DataType == typeof(DateTime))
{
dv.RowFilter = colname +=#+ value + #;
}
}
I'm working on a web application in C#. I want to filter a GridView with selecting a column from a listBox and then instantly filter the table accordingly to the text the user typed into a textBox using RowFilter.
I already managed nearly the whole part, but I've stuck trying to solve the comparison for numbers and dates.
The comparison for text works well:
DataTable dt = new DataTable();
dt = GridView1.DataSource as DataTable;
dt.DefaultView.RowFilter = string.Format(ListBox1.SelectedItem.Text + " LIKE '%{0}%'", textBox1.Text);
For numbers I tried something like this (didn't work):
dt.DefaultView.RowFilter = string.Format(ListBox1.SelectedItem.Text + " = '#{0}#'", textBox1.Text);
I guess comparison for numbers and dates will be pretty much the same as for text?
I think the following will be helpful to you...
string colname = ListBox1.SelectedItem.Text;
string value = textBox1.Text;
if (colname != null && dt.Columns[colname] != null)
{
if ("Byte,Decimal,Double,Int16,Int32,Int64,SByte,Single,UInt16,UInt32,UInt64,".Contains(dt.Columns[colname].DataType.Name + ","))
{
dv.RowFilter = colname + "=" + value;
}
else if (dt.Columns[colname].DataType == typeof(string))
{
dv.RowFilter = string.Format(colname + " LIKE '%{0}%'", value);
}
else if (dt.Columns[colname].DataType == typeof(DateTime))
{
dv.RowFilter = colname + " = #" + value + "#";
}
}
这篇关于如何使用RowFilter过滤GridView中的数字和日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!