locked
Filter Gridview Data To Show only Required Data RRS feed

  • Question

  • User2033107836 posted

    Hello

    I need asp.net 2.0 C# code based on my requirments

    This is my table data

    Options Arrival Date Quantity Taken Date RefNo
    Select 01-Feb-18 10 10-Feb-18 K0001
    Select 01-Feb-18 10 03-Feb-18
    Select 02-Feb-18 20 04-Feb-18
    Select 04-Feb-18 30 04-Feb-18
    Select 04-Feb-18 30 13-Feb-18
    Select 04-Feb-18 30 15-Feb-18 K0003
    Select 05-Feb-18 15 16-Feb-18
    Select 05-Feb-18 15 17-Feb-18

    I need result set like this 

    Options Arrival Date Quantity Taken Date RefNo
    Select 01-Feb-18 10 10-Feb-18 K0001
    Select 02-Feb-18 20 04-Feb-18
    Select 04-Feb-18 30 15-Feb-18 K0003
    Select 05-Feb-18 15 17-Feb-18

    how to do so...

    Sunday, February 4, 2018 4:23 PM

Answers

  • User-707554951 posted

    Hi asp.ambur

    Working code as below:

     protected void Page_Load(object sender, EventArgs e)
            {
    
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[5] { new DataColumn("Options", typeof(string)),
                        new DataColumn("Arrival Date", typeof(string)),
                        new DataColumn("Quantity",typeof(int)),
                    new DataColumn("Taken Date",typeof(string)),
                 new DataColumn("RefNo",typeof(string)), });
                dt.Rows.Add("Select", "01-Feb-18", 10, "10-Feb-18", "K0001");
                dt.Rows.Add("Select", "01-Feb-18", 10, "03-Feb-18", "");
                dt.Rows.Add("Select", "02-Feb-18", 20, "04-Feb-18", "");
                dt.Rows.Add("Select", "04-Feb-18", 30, "04-Feb-18", "");
                dt.Rows.Add("Select", "04-Feb-18", 30, "13-Feb-18", "");
                dt.Rows.Add("Select", "04-Feb-18", 30, "15-Feb-18", "K0003");
                dt.Rows.Add("Select", "05-Feb-18", 15, "16-Feb-18", "");
                dt.Rows.Add("Select", "05-Feb-18", 15, "17-Feb-18", "");
                List<string> ArrivalDate = new List<string>();
    
                foreach (DataRow row in dt.Rows)
                {
                    if (!ArrivalDate.Exists(s => s == row[1].ToString()))
                    {
                        if (!ArrivalDate.Exists(s => s == row[1].ToString()))
                            ArrivalDate.Add(row[1].ToString());
                    }
                }
                var ss = ArrivalDate;
                DataTable dt2 = new DataTable();
                dt2.Columns.AddRange(new DataColumn[5] { new DataColumn("Options", typeof(string)),
                        new DataColumn("Arrival Date", typeof(string)),
                        new DataColumn("Quantity",typeof(int)),
                    new DataColumn("Taken Date",typeof(string)),
                 new DataColumn("RefNo",typeof(string)), });
                foreach (string da in ArrivalDate)
                {
                    List<string> TakenDate = new List<string>();
                    foreach (DataRow row in dt.Rows)
                    {
                        if (row[1].ToString() == da)
                        {
                            TakenDate.Add(row[3].ToString());
                        }
                    }
                    int   lastTakenDate = int.MinValue;
                    string lastTakenDatestr=null;
                    foreach (string TakenDateStr in TakenDate)
                    {
                        if ( int.Parse( TakenDateStr.Split('-')[0].TrimStart('0'))> lastTakenDate)
                        {
                            lastTakenDate = int.Parse(TakenDateStr.Split('-')[0].TrimStart('0'));
                            lastTakenDatestr = TakenDateStr;
                        }
                    }
                    string sdebug = lastTakenDatestr;
    
    
                    foreach (DataRow row in dt.Rows)
                    {
                        if(row[3].ToString()== lastTakenDatestr.ToString()&& row[1].ToString()== da)
                        {
                            dt2.Rows.Add(row.ItemArray);
                        }
                    }
    
            }
            GridView1.DataSource = dt2;
                GridView1.DataBind();
            }

    Ouput:

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 5, 2018 11:06 AM

All replies

  • User-707554951 posted

    Hi asp.ambur

    Working code as below:

     protected void Page_Load(object sender, EventArgs e)
            {
    
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[5] { new DataColumn("Options", typeof(string)),
                        new DataColumn("Arrival Date", typeof(string)),
                        new DataColumn("Quantity",typeof(int)),
                    new DataColumn("Taken Date",typeof(string)),
                 new DataColumn("RefNo",typeof(string)), });
                dt.Rows.Add("Select", "01-Feb-18", 10, "10-Feb-18", "K0001");
                dt.Rows.Add("Select", "01-Feb-18", 10, "03-Feb-18", "");
                dt.Rows.Add("Select", "02-Feb-18", 20, "04-Feb-18", "");
                dt.Rows.Add("Select", "04-Feb-18", 30, "04-Feb-18", "");
                dt.Rows.Add("Select", "04-Feb-18", 30, "13-Feb-18", "");
                dt.Rows.Add("Select", "04-Feb-18", 30, "15-Feb-18", "K0003");
                dt.Rows.Add("Select", "05-Feb-18", 15, "16-Feb-18", "");
                dt.Rows.Add("Select", "05-Feb-18", 15, "17-Feb-18", "");
                List<string> ArrivalDate = new List<string>();
    
                foreach (DataRow row in dt.Rows)
                {
                    if (!ArrivalDate.Exists(s => s == row[1].ToString()))
                    {
                        if (!ArrivalDate.Exists(s => s == row[1].ToString()))
                            ArrivalDate.Add(row[1].ToString());
                    }
                }
                var ss = ArrivalDate;
                DataTable dt2 = new DataTable();
                dt2.Columns.AddRange(new DataColumn[5] { new DataColumn("Options", typeof(string)),
                        new DataColumn("Arrival Date", typeof(string)),
                        new DataColumn("Quantity",typeof(int)),
                    new DataColumn("Taken Date",typeof(string)),
                 new DataColumn("RefNo",typeof(string)), });
                foreach (string da in ArrivalDate)
                {
                    List<string> TakenDate = new List<string>();
                    foreach (DataRow row in dt.Rows)
                    {
                        if (row[1].ToString() == da)
                        {
                            TakenDate.Add(row[3].ToString());
                        }
                    }
                    int   lastTakenDate = int.MinValue;
                    string lastTakenDatestr=null;
                    foreach (string TakenDateStr in TakenDate)
                    {
                        if ( int.Parse( TakenDateStr.Split('-')[0].TrimStart('0'))> lastTakenDate)
                        {
                            lastTakenDate = int.Parse(TakenDateStr.Split('-')[0].TrimStart('0'));
                            lastTakenDatestr = TakenDateStr;
                        }
                    }
                    string sdebug = lastTakenDatestr;
    
    
                    foreach (DataRow row in dt.Rows)
                    {
                        if(row[3].ToString()== lastTakenDatestr.ToString()&& row[1].ToString()== da)
                        {
                            dt2.Rows.Add(row.ItemArray);
                        }
                    }
    
            }
            GridView1.DataSource = dt2;
                GridView1.DataBind();
            }

    Ouput:

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 5, 2018 11:06 AM
  • User2033107836 posted

    Hello

    Error

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS1525: Invalid expression term '>'

    Source Error:

    Line 291:          foreach (DataRow row in dt.Rows)
    Line 292:            {
    Line 293:                if (!ArrivalDate.Exists(s => s == row[1].ToString()))
    Line 294:                {
    Line 295:                    if (!ArrivalDate.Exists(s => s == row[1].ToString()))

    Tuesday, February 6, 2018 8:43 AM
  • User2033107836 posted

    Hello Cathy Zou

    I'm using asp.net 2.0 C# 

    Please check the code.. error

    Tuesday, February 6, 2018 8:46 AM
  • User-707554951 posted

    Hi asp.ambur

    I also use 2.0

    no error on my side,

    Lambda expression are supported starting version 3.0 of the C# language. The framework 2.0 + Vs 2005 pair uses the c# 2.0.

    Please use VS 2015 OR 2017

    Best regards 

    Cathy

    Tuesday, February 6, 2018 8:53 AM