DataView with DateTime comparation

I have a DataTable which has a DateTime column .

I want to view in my DataGrid only the rows that has any date and their time is between 12:00 and 16:00 how can i compare only the time



Answer this question

DataView with DateTime comparation

  • Pintu Shukla

    What you do is create a DataView over the DataTable and set the DataView's RowFilter property to some expression that will filter down the results. Then attach the DataView to the DataGrid.

    So DataView is like filter you can layer over the DataTable.

    If you have a DateTime you can use the Hour property to get the hours:

    DateTime dt1 = DateTime.Now;

    if (dt1.Hour > 12 && dt1.Hour < 16)

    {

    // Do something

    }



  • Autofreak

    Yeah something like:

    RowFilter = "[datefield].Hours >=12 and [datefield].Hours >=16"

    I'm a little fuzzy on the details of RowFilter syntax so don't quote me. (G)



  • 40th Floor

    Yes the expression syntax is somewhat limited.

    I'll see what I can find out and post back.



  • argodev

    It dosen't work...
  • Chris Jackson

    But how can I set my RowFilter property that will return me only the hourse between 12 and 16
  • Jensenuk

    Ahhh OK i understand u

    ThanX :-)


  • p_metzler

    I got it!!!!!!!!!!!!!!

    I found this link

    DataColumn.Expression Property

    This is my final code:

    DataView dv = new DataView(ds.Tables[0]);

    dv.RowFilter = "CONVERT(SUBSTRING((CONVERT(MyDateTimeColumn,System.String)),12,19),System.DateTime) >= '08:00' AND " +

    "CONVERT(SUBSTRING((CONVERT(MyDateTimeColumn,System.String)),12,19),System.DateTime) <= '20:30'";


  • Bayamo

    Hi there,

    I have a similar problem but it is comparing dates only.... The error is invalid cast from Double to DateTime... I am using sql server 2005, this is my code

    dv.RowFilter = "Date_DT = CONVERT(" & dtmDateVar & " ,'System.DateTime')"

    please help me....



  • dirtydozen

    DataColumn.Expression Property

    If the link doesn't work this is the link

    ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemdatadatacolumnclassexpressiontopic.htm


  • coyoteworks

    Hi again,

    I found the answer this is very useful: try to look at this link below:

    http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx FeedbackID=95799



  • p33

    It workes only when the dates are equals....

    Is there a way to do this with >,< with converting to DateTime

    is there a way to do somthing like this in TSQL but in my row filter

    CONVERT(VARCHAR,myDateTime,103)


  • andris11

    Ok, I had to do some hard thinking about this, so you owe me one.(G)

    RowFilter has limited set of operators, but they are designed this way so they can be fast an not require compiling a dynamic assembly (like regex does for example).

    So simple and fast.

    So to do what you want, I came up with this RowFilter (this is one big string):

    Convert(MyDate,'System.String') like '*12:*' or

    Convert(MyDate,'System.String') like '*13:*' or

    Convert(MyDate,'System.String') like '*14:*' or

    Convert(MyDate,'System.String') like '*15:*' or

    Convert(MyDate,'System.String') like '*16:*'

    Thar you have it!



  • sdiaconescu2001

    I was in a similar situation and your posts helped get me on the right path.

    I needed to just compare the month and year part of a dataview filter to a value.

    I used something like this:

    filtertext = " (Convert(DATE_ORDER_RECEIVED,System.String) like '" +

    filterDateRecMonthfromDDL + "/%" + "')";

    Since my DATE_ORDER_RECEIVED field value was coming from a sql server 2005 datetime field the month is always cropped to 1 digit for jan thru september (no 01-jan, 02 - feb, but 1-jan, 2-feb) and since I exclude the '%' from the beginning of my value i am filtering on it will only go from the first part of the string which is the month (mm/dd/yyyy).


  • YoungJoe

    You are my king man!!!!!!!!!

    This is just what i was searching for!!!

    by I have used this code instead:

    DataView dv = new DataView(ds.Tables[0]);

    dv.RowFilter = "Convert(ccc,'System.DateTime') >= '12:00' AND" +

    " Convert(ccc,'System.DateTime') <= '16:00'";

    dataGrid1.DataSource = dv;

    and it worked like a charm ...


  • DataView with DateTime comparation