Hi, thanks for all your help so far, but I think I hit the bottom of
the problem. (I do hope that is the case.)
First of all, there is the bug:
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=95799
Apparently the code used to compare DateTime are culture dependent
(fair enough). However it is interesting to note that the sample code
in the link above (re-pasted below) actually raises an exception:
try {
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("Customers");
dt.Locale = new CultureInfo("en-GB");
dt.Columns.Add("CustId", typeof(int));
dt.Columns.Add("OrderDate", typeof(DateTime));
DateTime dateTime = new DateTime(2004, 1, 20);
dt.Rows.Add(new object[] { 1, dateTime });
DataView dv = dt.DefaultView;
string s = String.Format(new
CultureInfo("en-GB").DateTimeFormat, "OrderDate = #{0:d}#", dateTime);
Console.WriteLine(s);
dv.RowFilter = s; // EXCEPTION RAISED HERE
Console.WriteLine(dv.Count);
} catch (Exception exc) {
Console.WriteLine(exc);
}
The exception raised is (when run in .NET 2.0).
System.FormatException: String was not recognized as a valid DateTime.
at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi,
DateTimeStyles styles)
at System.DateTime.Parse(String s, IFormatProvider provider)
at System.Data.ConstNode..ctor(DataTable table, ValueType type,
Object constant, Boolean fParseQuotes)
at System.Data.ExpressionParser.Parse()
at System.Data.DataExpression..ctor(DataTable table, String
expression, Type type)
at System.Data.DataView.set_RowFilter(String value)
Now I don't know why the exception occurs, but I'll leave it up to them
Microsoft guys to work it out.
Amar's suggestion seems to be the only workable solution to me (at
least for now). However, there is a catch to using single quotes (')
instead of hashes (#) to surround the date:
The DateTime string has to be formated using the locale of the
DataTable.
What lead me to this is the fact that our application software used
single quotes to specify DateTime in RowFilter string, and this has
worked fine in .NET 1.1. But recently we tested it on .NET 2.0 and it
is broken, receiving a System.Data.EvaluateException:
System.Data.EvaluateException: Cannot perform '=' operation on
System.DateTime and System.String.
After realising everything is (and should be) culture sensitive, it
turned out the problem was that the DataTable.Locale property was en-US
while the DateTime was formatted with en-AU. So what Amar says is
correct, provided you format the DateTime with the CultureInfo from
DateTime.Locale.
Hopefully this will be all.
Lawry.
> Hi,
> If you just want to compare against the datetime column you can
[quoted text clipped - 5 lines]
> This works as desired.
> Hope this helps..