locked
combined datarows with the same values RRS feed

  • Question

  • User2108892867 posted

    Hello everyone, I have this datatable that I built from a gridview:

    DataTable dt = new DataTable();
                dt.Columns.Add("productId", typeof(string));
                dt.Columns.Add("unitId", typeof(string));
                dt.Columns.Add("price", typeof(string));
                dt.Columns.Add("quantity", typeof(string));
    
                foreach (GridViewRow r in GridView1.Rows)
                {
                    TextBox txtQuantity = r.FindControl("txtQuantity") as TextBox;
                    TextBox txtPrice= r.FindControl("txtPrice") as TextBox;
                    DropDownList ddlUnit = r.FindControl("ddlUnit") as DropDownList;
                    string productTypeId= GridView1.DataKeys[r.RowIndex].Value.ToString();
                        DataRow row;
                        row = dt.NewRow();
                        row["productId"] = productTypeId;
                        row["unitId"] = ddlUnit.SelectedValue;
                        row["price"] = txtPrice.Text;
                        row["quantity"] = txtQuantity.Text;
                        dt.Rows.Add(row);
                }

    After I have this datatable, I would like to loop through each row in the datatable and combine rows with the same product id, unit id and price by adding to the quality. How could I do that? In SQL, it should be something like this 

    Select SUM(quantity) FROM table group by productId, unitId, price

    Thanks. 

    Sunday, June 30, 2019 11:11 PM

Answers

  • User288213138 posted

    Hi asplearning,

    According to your description,  I made a demo for you.

    I traverse the data in the table twice. The first one is to read all the data in the table.

    The second one is to judge whether the value in target table is the same as that in the first one. If so, combine them.

    The code:

    Aspx.cs:
     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    Bind();
                }
            }
    
            public void Bind()
            {
                DataTable table = new DataTable();
                table.Columns.Add(new DataColumn("productId", typeof(string)));
                table.Columns.Add(new DataColumn("unitId", typeof(string)));
                table.Columns.Add(new DataColumn("price", typeof(string)));
                table.Columns.Add(new DataColumn("quantity", typeof(string)));
                table.Rows.Add("1", "u1", "p1", "1");
                table.Rows.Add("1", "u1", "p1", "1");
                table.Rows.Add("2", "u2", "p2", "1");
                DataTable table1 = new DataTable();
                table1.Columns.Add(new DataColumn("productId", typeof(string)));
                table1.Columns.Add(new DataColumn("unitId", typeof(string)));
                table1.Columns.Add(new DataColumn("price", typeof(string)));
                table1.Columns.Add(new DataColumn("quantity", typeof(string)));
    
                foreach (DataRow item in table.Rows)
                {
                    bool hasRow = false;
                    foreach (DataRow item2 in table1.Rows)
                    {
                        if (item2["productId"].ToString() == item["productId"].ToString() & item2["unitId"].ToString() == item["unitId"].ToString() & item2["price"].ToString() == item["price"].ToString())
                        {
                            item2["productId"] = item2["productId"];
                            item2["unitId"] = item2["unitId"];
                            item2["price"] = item2["price"];
                            item2["quantity"] = Convert.ToInt32(item2["quantity"]) + Convert.ToInt32(item["quantity"]);
                            hasRow = true;
                            break;
                        }
                    }
                    if (!hasRow)
                    {
                        table1.Rows.Add(item["productId"], item["unitId"], item["price"], item["quantity"]);
    
                    }
                }
                GridView1.DataSource = table;
                GridView1.DataBind();
    
                GridView2.DataSource = table1;
                GridView2.DataBind();
            }
    Aspx:
     <div>
                <asp:GridView ID="GridView1" runat="server"></asp:GridView><br /><br />
            
            <asp:GridView ID="GridView2" runat="server"></asp:GridView>
            </div>

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 1, 2019 7:29 AM