0

我有一个从 linq 到 sql 查询的 gridview,我想用文本框中的值更新这个 gridview 的列。在这个 gridview 中有一些有折扣的产品,我在 linq to sql 查询中选择了一些产品,然后我想更改他们的折扣。我在文本框中插入新的折扣,然后按“保存更改”按钮,我想用新的折扣更新产品的折扣。我的代码:

    protected void btnDisplay_Click(object sender, ImageClickEventArgs e)
    {
        OrdersDataContext contex = new OrdersDataContext();

        var query2 = from d in contex.Discounts
                     join r in contex.Registrations on d.id_reg equals r.id
                     join pr in contex.Products on d.id_prod equals pr.id
                     join p in contex.pTs on pr.pId equals p.id
                     join dim in contex.dimTs on p.dimId equals dim.id
                     join t in contex.typeTs on dim.typeId equals t.id
                     join c in contex.codeTs on t.codeId equals c.id
                     where (r.fullname == txtCompName.Text && r.password == txtpass.Text && c.code == TextBox1.Text && t.type == TextBox2.Text && dim.dimension == TextBox3.Text) 
                     select new
                     {
                         CompanyName = r.fullname,
                         Article = pr.article,
                         Description = pr.description,
                         Price = pr.price,
                         Discount = d.discounts
                     };

       GridViewDiscounts.DataSource = query2.ToList();
       GridViewDiscounts.DataBind();

    }



    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        OrdersDataContext contex = new OrdersDataContext();

        if (GridViewDiscounts.Rows.Count != 0)
        {
            foreach (GridViewRow row in GridViewDiscounts.Rows)
            {
               //here is my problem!!!!
            }
        }

        ClearDetails();
        string Msg = "<script>alert('Data updated successfully!');</script>";
        ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "alert", Msg, false);
    }


}

}

任何人都可以帮助我吗???

4

1 回答 1

0

我解决了,这是答案:

protected void btnSave_Click(object sender, ImageClickEventArgs e)
{
    OrdersDataContext contex = new OrdersDataContext();
    var query = from d in contex.Discounts
    join r in contex.Registrations on d.id_reg equals r.id
    join pr in contex.Products on d.id_prod equals pr.id
    join p in contex.pTs on pr.pId equals p.id
    join dim in contex.dimTs on p.dimId equals dim.id
    join t in contex.typeTs on dim.typeId equals t.id
    join c in contex.codeTs on t.codeId equals c.id
    where (r.fullname == txtCompName.Text && r.password == txtpass.Text && c.code == TextBox1.Text && t.type == TextBox2.Text && dim.dimension == TextBox3.Text)
    select new
    {
        prodId = d.id_prod,
        regId = d.id_reg,
        discount = d.discounts
    };
    foreach (var data in query)
    {
        TextBox4.Text = String.Join(Environment.NewLine, data.prodId);
        TextBox5.Text = String.Join(Environment.NewLine, data.regId);
        Discount dis = contex.Discounts.Single(d => d.id_prod == Int32.Parse(TextBox4.Text) && d.id_reg == Int32.Parse(TextBox5.Text));
        dis.discounts = txtnewdiscount.Text;
    }
    contex.SubmitChanges();
    RefreshGrid();
    string Msg = "<script>alert('Data updated successfully!');</script>";
    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "alert", Msg, false);
}
于 2012-07-19T10:59:03.027 回答