You must Sign In to post a response.
  • Category: Windows

    How to filter datatable using date

    Name LastUpdateDate
    harni 03/25/2016 09:07:20
    seetha 03/25/2016 03:07:20
    vision 03/26/2016 09:06:20
    abc 03/24/2016 09:09:20
    def 03/27/2016 09:07:20
    ghi 03/27/2016 08:07:20
    jkl 03/28/2016 09:09:20
    mno 03/28/2016 09:06:20
    pqr 03/29/2016 05:07:20



    Abouve tablular data in my Datatable.

    Required : I want to get rows separately according to date passed.

    Expected result :

    eg)

    March 25

    Name LastUpdateDate
    harni 03/25/2016 09:07:20
    seetha 03/25/2016 03:07:20


    March 28

    Name LastUpdateDate
    jkl 03/28/2016 09:09:20
    mno 03/28/2016 09:06:20

    Im expecting something like

    Date date= ( All dates alone without timing)

    dataTable.Select("LastUpdateDate like%" + date + "%");
  • #765309
    Hi,

    You can do it in 2 ways, one is in database wise and another one is in C# code wise.

    If you want to filter the result only date wise then you should convert the result set into date format only, as per output provide by you it is in datetime format.

    If you want to convert the result in date format using SQL query refer below sample query for the same.


    select convert(date, getdate())


    If you want to convert the datatable result in date format, using .ToString() you can convert the result as you want.


    foreach(DataRow dr in dt.Rows)
    {
    string date=dr["datefield"].ToString("dd/MMM/yyyy");
    }


    After get the data in your expected format then filter it.

    I provide you the sample you have to recode as you want.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #765310
    I have below method to filter date from datatable, see below snippet, it filters using LINQ

    private DataTable FilterTable(DataTable table, DateTime startDate, DateTime endDate)
    {
    var filteredRows =
    from row in table.Rows.OfType<DataRow>()
    where (DateTime)row[0] >= startDate
    where (DateTime)row[0] <= endDate
    orderby (int)row[1] descending
    select row;
    var filteredTable = table.Clone();
    filteredRows.ToList().ForEach(r => filteredTable.ImportRow(r));
    return filteredTable;
    }

    I have not try it though.

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #767636
    I am using windows hosting from "mywindowshosting (dot) com". but i have not much more idea about this because i am a new user.

  • #767643
    Hi

    you can try this code



    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("LastUpdateDate") });
    dt.Rows.Add(1, "harni", "03/25/2016 09:07:20");
    dt.Rows.Add(2, "seetha", "03/25/2016 09:07:20");
    dt.Rows.Add(3, "vision", "03/26/2016 09:06:20");
    dt.Rows.Add(4, "abc", "03/24/2016 09:09:20");
    dt.Rows.Add(5, "def", "03/27/2016 09:07:20");
    dt.Rows.Add(6, "ghi", "03/27/2016 09:07:20");
    dt.Rows.Add(7, "jkl", "03/28/2016 09:07:20");
    dt.Rows.Add(8, "mno", "03/28/2016 09:07:20");
    dt.Rows.Add(9, "pqr", "07/29/2016 09:07:20");

    string date = "03/25/2016";

    //dt.Select("LastUpdateDate like '%" + date + "%'");
    //dt.DefaultView.RowFilter = " LastUpdateDate like '% " + date + " %'";
    //dt.DefaultView.RowFilter = " LastUpdateDate= #03/25/2016# ";
    DataTable Dt = new DataTable();
    DataView dv = new DataView();
    dv = dt.DefaultView;
    dv.RowFilter = " LastUpdateDate >=#'" + date + "' And LastUpdateDate<= #'" + date + "'";
    Dt = dv.ToTable();


    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.


Sign In to post your comments