DataView

  • Thread starter Thread starter Lloyd Dupont
  • Start date Start date
L

Lloyd Dupont

I have a DataTable with a DateTime column.
in this data time column I am only interested in the time, like 8:30 AM
how could I create a DataView of this DataTable where this column = 8:
something (that is 8:00 would be as valid as 8:31, but 9:01 won't)
 
Hi Lloyd,

Actually, there is a way.
First you have to set datetime formatting to a fixed width:
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern =
"dd.MM.yyyy";

Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongTimePattern =
"hh-mm";

Then the rowfilter argument would be:

DataView dv = new DataView(t, "SUBSTRING(CONVERT(tm, System.String), 12, 2)
= 8", string.Empty, DataViewRowState.CurrentRows);

I extract the hour part from datetime string and compare it to 8.

HTH,
 
thanks again Miha !

Miha Markic said:
Hi Lloyd,

Actually, there is a way.
First you have to set datetime formatting to a fixed width:
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern =
"dd.MM.yyyy";

Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongTimePattern =
"hh-mm";

Then the rowfilter argument would be:

DataView dv = new DataView(t, "SUBSTRING(CONVERT(tm, System.String), 12, 2)
= 8", string.Empty, DataViewRowState.CurrentRows);

I extract the hour part from datetime string and compare it to 8.

HTH,
 
Back
Top