DataView RowFilter with TimeSpan DataType

自闭症网瘾萝莉.ら 提交于 2019-12-11 03:13:43

问题


I try to use the DataView RowFilter for a column with DataType "TimeSpan" as following:

dv.RowFilter = ("Convert([time],System.String) LIKE '17:12:00'")

I've found that the search Parameter "%17% or %12%, for the double Zeros i have to use a single one: %0%, works fine, now im not sure about the Convert(timespan, System.String) Format... . I know that the TimeSpan have a Special Format like (17,12,0) or {17}{12}{0} but as a not specified convert to string it should be: hh:mm:ss like timespan.ToString() - but with the DataView's RowFilter i can't get this to work!

I'm using Visual Studio 2008 Pro with .NET 3.5.


回答1:


The problem lies with the conversion to string. It produces a string formatted in a very strange way. To illustrate, let's create a table using this C# code:

        var table = new DataTable();
        table.Columns.Add("span", typeof(TimeSpan));
        table.Rows.Add(new object[] { new TimeSpan(1, 2, 3) });
        table.Rows.Add(new object[] { new TimeSpan(4, 5, 6) });
        table.Columns.Add("asstring");
        table.Columns["asstring"].Expression = "Convert(span, 'System.String')";

After we assign it to a grid control, it will look like this:

In Microsoft's own docs, they say to look at the Expression docs to see how Convert(span, 'System.String') works in RowFilter. That means that it converts the TimeSpan to exactly what you see in the screenshot - 01:02:03 becomes PT1H2M3S.




回答2:


To put it all together, you may use the following approach to filter columns of type TimeSpan in a DataTable for certain values. The idea behind is to convert the stored TimeSpan on the fly into its "database" string representation which in turn enables you to perform a string comparison with a TimeSpan value of your choice right after converting it into its "database" string representation.

Creation of the "database" string representation of a TimeSpan value requires special attention to time segments having zero values. It's specification could be described by the following pattern:

PT[<hours>H][<minutes>M]<seconds>[.<fraction>]S

where <fraction> refers to the ten-millionths of a second, see "Custom TimeSpan Format Strings" of the MSDN documentation. Time segments having zero values need to be omitted, except for the seconds. Also the fraction part is expected to not contain any trailing zero digit. Finally, the string is enclosed by a prefix PT and a postfix S.

This leads to the following format string to create a correct "database" string representation:

TimeSpan oTimeSpan;  // TimeSpan value of your choice
string strTimeSpan =
    string.Format("PT{0}{1}{2}{3}S",
        (oTimeSpan.Hours == 0
            ? ""
            : string.Format("{0:%h}H", oTimeSpan)),
        (oTimeSpan.Minutes == 0
            ? ""
            : string.Format("{0:%m}M", oTimeSpan)),
        string.Format("{0:%s}", oTimeSpan),
        string.Format(".{0:fffffff}", oTimeSpan).TrimEnd('0', '.'))

The expression to filter columns of type TimeSpan in a DataTable for certain values could be now:

string strExpr =
    "Convert([ColumnName], '" + typeof(string).ToString() + "')" 
    + "='" + strTimeSpan + "'";

where ColumnName refers to the name of the DataTable column containing the TimeSpan values.

Note that this approach is based on a string comparison, thus, only compare for equality would provide reliable results. Other compare operations like "greater than" or "less than" will most likely produce unexpected results.



来源:https://stackoverflow.com/questions/14530286/dataview-rowfilter-with-timespan-datatype

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