locked
Merge Grid view data on select RRS feed

  • Question

  • User2033107836 posted
    Hi
    This is my Gridview 1 data
    Ref No ItemName Date Qty Amount Per Qty Amount
    R1 Apple 01-Dec-17 2 500 250
    R1 Mango 12-Dec-17 6 300 50
    R2 Banana 05-Dec-217 10 20 2
    R3 Apple 09-Dec-217 5 50 10
    I have one drodpwnlist in that if I click single then it should show data same above in gridview
    If I select Merge then it should show like this 
    Ref No ItemName Date Qty Amount Per Qty Amount
    R1 Apple, Mango 01-12-2017,12-Dec-2017 8 800 300
    R2 Banana 05-Dec-217 10 20 2
    R3 Apple 09-Dec-217 5 50 10
    How To do using asp.net 2.0 C#
    Thanks
    Friday, December 22, 2017 10:10 AM

Answers

  • User283571144 posted

    Hi asp.ambur,

    Compiler Error Message: CS0246: The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)

    Please try this:

        public partial class WebForm1 : System.Web.UI.Page
        {
            DataTable SingleTable;
            DataTable MergeTable;
            protected void Page_Load(object sender, EventArgs e)
            {
                SingleTable = new DataTable();
                SingleTable.Columns.Add("RefNo");
                SingleTable.Columns.Add("ItemName");
                SingleTable.Columns.Add("Date");
                SingleTable.Columns.Add("Qty");
                SingleTable.Columns.Add("Amount");
                SingleTable.Columns.Add("Per Qty Amount");
    
                MergeTable = new DataTable();
                MergeTable.Columns.Add("RefNo");
                MergeTable.Columns.Add("ItemName");
                MergeTable.Columns.Add("Date");
                MergeTable.Columns.Add("Qty");
                MergeTable.Columns.Add("Amount");
                MergeTable.Columns.Add("Per Qty Amount");
    
                SingleTable.Rows.Add("R1", "Apple", "01-Dec-17", 2, 500, 250);
                SingleTable.Rows.Add("R1", "Mango", "12-Dec-17", 6, 300, 50);
                SingleTable.Rows.Add("R2", "Banana", "05-Dec-17", 10, 20, 2);
                SingleTable.Rows.Add("R3", "Apple", "05-Dec-17", 5, 50, 10);
    
                //var result = from row in SingleTable.AsEnumerable()
                //             group row by row.Field<string>("Ref No") into grp
                //             select new
                //             {
                //                 gr = grp.ToList()
                //             };
    
                
                List<DataRow[]> result = new List<DataRow[]>();
                DataTable dtName = SingleTable.DefaultView.ToTable(true, "RefNo");
                for (int i = 0; i < dtName.Rows.Count; i++)
                {
                    string query = "RefNo='" + dtName.Rows[i][0] + "'";
                    DataRow[] rowas = SingleTable.Select(query);
                     result.Add(rowas);
                }
    
    
                foreach (DataRow[] item in result)
                {
                    DataRow d1 = MergeTable.NewRow();
    
                    for (int i = 0; i < MergeTable.Columns.Count; i++)
                    {
                        int count;
                        int Numre = 0;
                        string Strre = String.Empty;
    
                        if (i == 0)
                        {
                            var r = item[i];
                            d1[i] = r[i];
                        }
                        else
                        {
                            foreach (DataRow dr in item)
                            {
                                if (Int32.TryParse(dr[i].ToString(), out count))
                                {
                                    Numre += count;
                                }
                                else
                                {
                                    Strre += dr[i];
                                }
                            }
                            if (string.IsNullOrEmpty(Strre))
                            {
                                d1[i] = Numre;
                            }
                            else
                            {
                                d1[i] = Strre;
                            }
                        }
    
                    }
                    MergeTable.Rows.Add(d1);
                }
    
            }
    
            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (DropDownList1.SelectedValue == "Single")
                {
                    GridView1.DataSource = SingleTable;
                    GridView1.DataBind();
                }
                else if (DropDownList1.SelectedValue == "Merge")
                {
                    GridView1.DataSource = MergeTable;
                    GridView1.DataBind();
                }
            }
        }

    Besides, could you please tell me which .net framework version you have used in asp.net 2.0?

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 27, 2017 2:26 AM

All replies

  • User283571144 posted

    Hi asp.ambur,

    According to your description, I suggest you could create a  dropdownlist change event. In this event you could rebind your gridview with the right datasource.

    About how to merge the same row, I suggest you could firstly using datatable's AsEnumerable method ,then you could use Linq group to merge the same row's value.

    More details, you could refer to below codes sample:

    Aspx:

        <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
                <asp:ListItem Text="Single" Value="Single"></asp:ListItem>
                 <asp:ListItem Text="Merge" Value="Merge">  </asp:ListItem>
            </asp:DropDownList>
                <hr />
            <asp:GridView ID="GridView1" runat="server"></asp:GridView>
        </div>
        </form>

    Code-behind:

        public partial class GrdiviewDropDownlistChange : System.Web.UI.Page
        {
            DataTable SingleTable;
            DataTable MergeTable;
            protected void Page_Load(object sender, EventArgs e)
            {
                SingleTable = new DataTable();
                SingleTable.Columns.Add("Ref No");
                SingleTable.Columns.Add("ItemName");
                SingleTable.Columns.Add("Date");
                SingleTable.Columns.Add("Qty");
                SingleTable.Columns.Add("Amount");
                SingleTable.Columns.Add("Per Qty Amount");
    
                MergeTable = new DataTable();
                MergeTable.Columns.Add("Ref No");
                MergeTable.Columns.Add("ItemName");
                MergeTable.Columns.Add("Date");
                MergeTable.Columns.Add("Qty");
                MergeTable.Columns.Add("Amount");
                MergeTable.Columns.Add("Per Qty Amount");
    
                SingleTable.Rows.Add("R1", "Apple", "01-Dec-17", 2, 500, 250);
                SingleTable.Rows.Add("R1", "Mango", "12-Dec-17", 6, 300, 50);
                SingleTable.Rows.Add("R2", "Banana", "05-Dec-17", 10, 20, 2);
                SingleTable.Rows.Add("R3", "Apple", "05-Dec-17", 5, 50, 10);
    
                var result = from row in SingleTable.AsEnumerable()
                             group row by row.Field<string>("Ref No") into grp
                             select new
                             {
                                 gr = grp.ToList()
                             };
    
                foreach (var item in result)
                {
                    DataRow d1 = MergeTable.NewRow();
                   
                    for (int i = 0; i < MergeTable.Columns.Count; i++)
                    {
                        int count;
                        int Numre = 0;
                        string Strre = String.Empty;
    
                        if (i==0)
                        {
                            var r =  item.gr.First();
                            d1[i] = r[i];
                        }
                        else
                        {
                            foreach (var dr in item.gr)
                            {
                                if (Int32.TryParse(dr[i].ToString(), out count))
                                {
                                    Numre += count;
                                }
                                else
                                {
                                    Strre += dr[i];
                                }
                            }
                            if (string.IsNullOrEmpty(Strre))
                            {
                                d1[i] = Numre;
                            }
                            else
                            {
                                d1[i] = Strre;
                            }
                        }
                  
                    }
                    MergeTable.Rows.Add(d1);
                }
            }
    
            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (DropDownList1.SelectedValue == "Single")
                {
                    GridView1.DataSource = SingleTable;
                    GridView1.DataBind();            }
                else if (DropDownList1.SelectedValue == "Merge")
                {
                    GridView1.DataSource = MergeTable;
                    GridView1.DataBind();
                }
     
            }
        }

    Results:

    Best Regards,

    Brando/

    Sunday, December 24, 2017 5:07 AM
  • User2033107836 posted
    Thanks Brandon zwz

    Can u convert this same code in asp.net 2.0 c#

    I'm using .net 2.0

    Thanks
    Sunday, December 24, 2017 5:28 AM
  • User283571144 posted

    Hi asp.ambur,

    Can u convert this same code in asp.net 2.0 c#

    I'm so sorry that I forget using asp.net 2.0. Since 2.0 doesn't support AsEnumerable method.

    I suggest you could try to use below codes, it will work well.

        public partial class WebForm1 : System.Web.UI.Page
        {
            DataTable SingleTable;
            DataTable MergeTable;
            protected void Page_Load(object sender, EventArgs e)
            {
                SingleTable = new DataTable();
                SingleTable.Columns.Add("RefNo");
                SingleTable.Columns.Add("ItemName");
                SingleTable.Columns.Add("Date");
                SingleTable.Columns.Add("Qty");
                SingleTable.Columns.Add("Amount");
                SingleTable.Columns.Add("Per Qty Amount");
    
                MergeTable = new DataTable();
                MergeTable.Columns.Add("RefNo");
                MergeTable.Columns.Add("ItemName");
                MergeTable.Columns.Add("Date");
                MergeTable.Columns.Add("Qty");
                MergeTable.Columns.Add("Amount");
                MergeTable.Columns.Add("Per Qty Amount");
    
                SingleTable.Rows.Add("R1", "Apple", "01-Dec-17", 2, 500, 250);
                SingleTable.Rows.Add("R1", "Mango", "12-Dec-17", 6, 300, 50);
                SingleTable.Rows.Add("R2", "Banana", "05-Dec-17", 10, 20, 2);
                SingleTable.Rows.Add("R3", "Apple", "05-Dec-17", 5, 50, 10);
    
                //var result = from row in SingleTable.AsEnumerable()
                //             group row by row.Field<string>("Ref No") into grp
                //             select new
                //             {
                //                 gr = grp.ToList()
                //             };
    
                
                List<DataRow[]> result = new List<DataRow[]>();
                DataTable dtName = SingleTable.DefaultView.ToTable(true, "RefNo");
                for (int i = 0; i < dtName.Rows.Count; i++)
                {
                    string query = "RefNo='" + dtName.Rows[i][0] + "'";
                    DataRow[] rowas = SingleTable.Select(query);
                     result.Add(rowas);
                }
    
    
                foreach (var item in result)
                {
                    DataRow d1 = MergeTable.NewRow();
    
                    for (int i = 0; i < MergeTable.Columns.Count; i++)
                    {
                        int count;
                        int Numre = 0;
                        string Strre = String.Empty;
    
                        if (i == 0)
                        {
                            var r = item[i];
                            d1[i] = r[i];
                        }
                        else
                        {
                            foreach (var dr in item)
                            {
                                if (Int32.TryParse(dr[i].ToString(), out count))
                                {
                                    Numre += count;
                                }
                                else
                                {
                                    Strre += dr[i];
                                }
                            }
                            if (string.IsNullOrEmpty(Strre))
                            {
                                d1[i] = Numre;
                            }
                            else
                            {
                                d1[i] = Strre;
                            }
                        }
    
                    }
                    MergeTable.Rows.Add(d1);
                }
    
            }
    
            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (DropDownList1.SelectedValue == "Single")
                {
                    GridView1.DataSource = SingleTable;
                    GridView1.DataBind();
                }
                else if (DropDownList1.SelectedValue == "Merge")
                {
                    GridView1.DataSource = MergeTable;
                    GridView1.DataBind();
                }
            }
        }

    Best Regards,

    Brando

    Sunday, December 24, 2017 6:02 AM
  • User2033107836 posted

    Hello P

    Please check this error

    error

    im using asp.net 2.0 C#

    Compiler Error Message: CS0246: The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)

    Source Error:

    Line 56: 
    Line 57: 
    Line 58:             foreach (var item in result)
    Line 59:             {
    Line 60:                 DataRow d1 = MergeTable.NewRow();
    Tuesday, December 26, 2017 10:20 AM
  • User283571144 posted

    Hi asp.ambur,

    Compiler Error Message: CS0246: The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)

    Please try this:

        public partial class WebForm1 : System.Web.UI.Page
        {
            DataTable SingleTable;
            DataTable MergeTable;
            protected void Page_Load(object sender, EventArgs e)
            {
                SingleTable = new DataTable();
                SingleTable.Columns.Add("RefNo");
                SingleTable.Columns.Add("ItemName");
                SingleTable.Columns.Add("Date");
                SingleTable.Columns.Add("Qty");
                SingleTable.Columns.Add("Amount");
                SingleTable.Columns.Add("Per Qty Amount");
    
                MergeTable = new DataTable();
                MergeTable.Columns.Add("RefNo");
                MergeTable.Columns.Add("ItemName");
                MergeTable.Columns.Add("Date");
                MergeTable.Columns.Add("Qty");
                MergeTable.Columns.Add("Amount");
                MergeTable.Columns.Add("Per Qty Amount");
    
                SingleTable.Rows.Add("R1", "Apple", "01-Dec-17", 2, 500, 250);
                SingleTable.Rows.Add("R1", "Mango", "12-Dec-17", 6, 300, 50);
                SingleTable.Rows.Add("R2", "Banana", "05-Dec-17", 10, 20, 2);
                SingleTable.Rows.Add("R3", "Apple", "05-Dec-17", 5, 50, 10);
    
                //var result = from row in SingleTable.AsEnumerable()
                //             group row by row.Field<string>("Ref No") into grp
                //             select new
                //             {
                //                 gr = grp.ToList()
                //             };
    
                
                List<DataRow[]> result = new List<DataRow[]>();
                DataTable dtName = SingleTable.DefaultView.ToTable(true, "RefNo");
                for (int i = 0; i < dtName.Rows.Count; i++)
                {
                    string query = "RefNo='" + dtName.Rows[i][0] + "'";
                    DataRow[] rowas = SingleTable.Select(query);
                     result.Add(rowas);
                }
    
    
                foreach (DataRow[] item in result)
                {
                    DataRow d1 = MergeTable.NewRow();
    
                    for (int i = 0; i < MergeTable.Columns.Count; i++)
                    {
                        int count;
                        int Numre = 0;
                        string Strre = String.Empty;
    
                        if (i == 0)
                        {
                            var r = item[i];
                            d1[i] = r[i];
                        }
                        else
                        {
                            foreach (DataRow dr in item)
                            {
                                if (Int32.TryParse(dr[i].ToString(), out count))
                                {
                                    Numre += count;
                                }
                                else
                                {
                                    Strre += dr[i];
                                }
                            }
                            if (string.IsNullOrEmpty(Strre))
                            {
                                d1[i] = Numre;
                            }
                            else
                            {
                                d1[i] = Strre;
                            }
                        }
    
                    }
                    MergeTable.Rows.Add(d1);
                }
    
            }
    
            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (DropDownList1.SelectedValue == "Single")
                {
                    GridView1.DataSource = SingleTable;
                    GridView1.DataBind();
                }
                else if (DropDownList1.SelectedValue == "Merge")
                {
                    GridView1.DataSource = MergeTable;
                    GridView1.DataBind();
                }
            }
        }

    Besides, could you please tell me which .net framework version you have used in asp.net 2.0?

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 27, 2017 2:26 AM
  • User2033107836 posted

    Hello

    I'm using asp.net 2.0

    Error

    Please check

    Compiler Error Message: CS0246: The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)

    Source Error:

    Line 71:                     if (i == 0)
    Line 72:                     {
    Line 73:                         var r = item[i];
    Line 74:                         d1[i] = r[i];
    Line 75:                     }
    Monday, January 1, 2018 1:05 PM
  • User283571144 posted

    Hi asp.ambur,

    Please replace the var to DataRow.

    Best Regards,

    Brando

    Tuesday, January 2, 2018 2:30 AM
  • User2033107836 posted


    Hello
    Thanks for your code

    Can I show merge data in ,

    For example Apple,Mango

    Sunday, January 7, 2018 7:22 PM