![](/img/trans.png)
[英]how to Save and Update data from GridView to database in ASP.Net C#?
[英]How to save multiple gridview row data to database in asp.net c#
我的項目中有一個gridview控件,我想在其中將多個gridview行保存回數據庫。 我如何通過循環保存所有行。 請幫助我克服這個問題。
//Save TestDetails
foreach (GridViewRow rw in GridView1.Rows)
{
var n = new TestDetail
{
ServiceId = Convert.ToInt32(GridView1.DataKeys[rw.RowIndex].Value),
Service = rw.Cells[1].Text.ToString(), //getting input error
Price = Convert.ToInt32(rw.Cells[2].Text.ToString()) //getting input error
};
using (var context = new DiagEntities())
{
context.TestDetail.Add(n);
context.SaveChanges();
}
}
如果您處於編輯模式,則可以檢查特定單元格的控件集合...它應該使包含該值的文本框成為上下文。
Service = ((TextBox)(rw.Cells[1].Controls[0]).Text.ToString(),
Price = Convert.ToInt32(((TextBox)(rw.Cells[1].Controls[0]).Text.ToString())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.