C# 如何使用RowFilter在GridView中过滤数字和日期?
我正在用C#开发一个web应用程序。我想通过从列表框中选择一列来过滤GridView,然后根据用户使用RowFilter在文本框中键入的文本立即过滤表 我几乎已经完成了整个部分,但我一直在努力解决数字和日期的比较问题 文本的比较效果很好:C# 如何使用RowFilter在GridView中过滤数字和日期?,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在用C#开发一个web应用程序。我想通过从列表框中选择一列来过滤GridView,然后根据用户使用RowFilter在文本框中键入的文本立即过滤表 我几乎已经完成了整个部分,但我一直在努力解决数字和日期的比较问题 文本的比较效果很好: DataTable dt = new DataTable(); dt = GridView1.DataSource as DataTable; dt.DefaultView.RowFilter = string.Format(ListBox1.Selected
DataTable dt = new DataTable();
dt = GridView1.DataSource as DataTable;
dt.DefaultView.RowFilter = string.Format(ListBox1.SelectedItem.Text + " LIKE '%{0}%'", textBox1.Text);
对于数字,我尝试了类似的方法(无效):
我想数字和日期的比较与文本的比较基本相同?请看一看
这些是DataGridView上RowFilter的语法规则,包含字符串、数字和日期
字符串
dataView.RowFilter = "Name = 'John'" // string value
dataView.RowFilter = "Name = 'John ''A'''" // string with single quotes "John 'A'"
dataView.RowFilter = String.Format("Name = '{0}'", "John 'A'".Replace("'", "''"));
dataView.RowFilter = "Year = 2008" // integer value
dataView.RowFilter = "Price = 1199.9" // float value
dataView.RowFilter = String.Format(CultureInfo.InvariantCulture.NumberFormat,
"Price = {0}", 1199.9f);
dataView.RowFilter = "Date = #12/31/2008#" // date value (time is 00:00:00)
dataView.RowFilter = "Date = #2008-12-31#" // also this format is supported
dataView.RowFilter = "Date = #12/31/2008 16:44:58#" // date and time value
dataView.RowFilter = String.Format(CultureInfo.InvariantCulture.DateTimeFormat,
"Date = #{0}#", new DateTime(2008, 12, 31, 16, 44, 58));
数字
dataView.RowFilter = "Name = 'John'" // string value
dataView.RowFilter = "Name = 'John ''A'''" // string with single quotes "John 'A'"
dataView.RowFilter = String.Format("Name = '{0}'", "John 'A'".Replace("'", "''"));
dataView.RowFilter = "Year = 2008" // integer value
dataView.RowFilter = "Price = 1199.9" // float value
dataView.RowFilter = String.Format(CultureInfo.InvariantCulture.NumberFormat,
"Price = {0}", 1199.9f);
dataView.RowFilter = "Date = #12/31/2008#" // date value (time is 00:00:00)
dataView.RowFilter = "Date = #2008-12-31#" // also this format is supported
dataView.RowFilter = "Date = #12/31/2008 16:44:58#" // date and time value
dataView.RowFilter = String.Format(CultureInfo.InvariantCulture.DateTimeFormat,
"Date = #{0}#", new DateTime(2008, 12, 31, 16, 44, 58));
日期
dataView.RowFilter = "Name = 'John'" // string value
dataView.RowFilter = "Name = 'John ''A'''" // string with single quotes "John 'A'"
dataView.RowFilter = String.Format("Name = '{0}'", "John 'A'".Replace("'", "''"));
dataView.RowFilter = "Year = 2008" // integer value
dataView.RowFilter = "Price = 1199.9" // float value
dataView.RowFilter = String.Format(CultureInfo.InvariantCulture.NumberFormat,
"Price = {0}", 1199.9f);
dataView.RowFilter = "Date = #12/31/2008#" // date value (time is 00:00:00)
dataView.RowFilter = "Date = #2008-12-31#" // also this format is supported
dataView.RowFilter = "Date = #12/31/2008 16:44:58#" // date and time value
dataView.RowFilter = String.Format(CultureInfo.InvariantCulture.DateTimeFormat,
"Date = #{0}#", new DateTime(2008, 12, 31, 16, 44, 58));
我认为以下内容将对您有所帮助
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 + "#";
}
}
它起作用了!非常感谢,这正是我想要的。对我帮助很大。@Dentox9哪些问题?在转换数字或日期并相互比较时出现问题。您的代码使用的是dataView,但我使用的是GridView,因此比较函数之间可能存在差异。然而,桑格拉姆的解决方案解决了这个问题。