[英]How to save multiple gridview row data to database in asp.net c#
I have a gridview control in my project in which i wanted to save multiple gridview rows back to the database. 我的项目中有一个gridview控件,我想在其中将多个gridview行保存回数据库。 How do i get save all rows through looping.
我如何通过循环保存所有行。 Please help me to overcome this problem.
请帮助我克服这个问题。
//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();
}
}
If you are in edit mode, you could check the control collection of the particular cell... it should context the textbox containing the value. 如果您处于编辑模式,则可以检查特定单元格的控件集合...它应该使包含该值的文本框成为上下文。
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.