我如何使用DateTime列过滤DataTable?

时间:2013-01-24 10:03:24

标签: c# listview datatable datarow

我想过滤一个数据表,它可以工作,但如果我搜索一个DateTime,我会收到一个错误。

这是我的代码。我做错了什么?

 DataTable tb = DataBaseManager.GetRadiusDataTable(radiusconnectionstring, "marksullivan"); 

DataRow[] filteredRows = tb.Select("AcctStartTime LIKE '%" + searchstring + "%' OR AcctStopTime LIKE '%" + searchstring + "%' OR FramedIPAddress LIKE '%" + searchstring + "%'");
tb = filteredRows.CopyToDataTable();
this.ListView.DataSource = tb;
this.ListView.DataBind();

AcctStartTime:日期时间 AcctStopTime:datetime FramedIPAddress:varchar

The error: The Operation 'Like' could not to System.DateTime and System.String execute.

我该怎么做?

1 个答案:

答案 0 :(得分:8)

尝试这样DateComparision in RowFilter

 string filter = "DateFrom > '" + daDateFrom + "' AND DateTo <= '" + daDateTo + "'";
tb.Select(filter)

或 来自DataRow filter Examples

  

日期值包含在清晰字符##中。日期格式   与DateTime.ToString()方法的结果相同   不变或英国文化。

[C#]

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