2012-06-29 45 views
0

我有一個從linq到sql查詢的gridview,我想用文本中的值更新此gridview的列框。在這個GridView是一些產品的折扣,我選擇了一些產品在LINQ到SQL查詢,然後我想改變他們的折扣。 Iam在文本框中插入新的折扣,按下按鈕「保存更改」,我想更新新產品的折扣。我的代碼:如何更新gridview中的一列與文本框的值c#asp.net 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); 
    } 


} 

}

誰能幫助我,請???

回答

0

我解決它,這是ansewer:

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); 
}