简体   繁体   中英

Filter DateTime in DataView does not filter minutes and second

I have the following code (a DataView that filtered by DateTime):

dv = new DataView(
        dataTable1,
        "DateTime >= '" + Convert.ToDateTime("7/5/2011 9:21:00 AM") + "'",
        "ID ASC", DataViewRowState.CurrentRows);

My datatable1 consist of the following data in the DateTime column:

  • A: "7/5/2011 9:20:59 AM"
  • B: "7/5/2011 9:21:00 AM"
  • C: "7/5/2011 9:21:01 AM"
  • D: "7/5/2011 9:22:00 AM"

Supposedly, only A should get filtered. But my code has A , B and C filtered as well, and only returns D .

What is wrong?

Try this

dv = new DataView(
    dataTable1,
    "CONVERT(DateTime, System.DateTime) >= '" + Convert.ToDateTime("7/5/2011 9:21:00 AM") + "'",
    "ID ASC", DataViewRowState.CurrentRows);

Try enclosing the date string with # . For example:

dv = new DataView(
        dataTable1,
        "DateTime >= #" + Convert.ToDateTime("7/5/2011 9:21:00 AM") + "#",
        "ID ASC", DataViewRowState.CurrentRows);

Remove the convert.todatetime .

You can try this

String date1 =String.Empty ;
date1 = "7/5/2011 9:21:00 AM";
dv = new DataView(dataTable1,"DateTime >= #" + date1 + "#", "ID ASC", DataViewRowState.CurrentRows);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM