简体   繁体   English

如何在ASP.NET C#中将多个gridview行数据保存到数据库

[英]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.

相关问题 如何将 GridView 中的数据保存和更新到 ASP.Net C# 中的数据库? - how to Save and Update data from GridView to database in ASP.Net C#? 将gridview的所有行保存到数据库asp.net C# - save all rows of gridview to database asp.net c# 如何使用asp.net C#中的绑定功能在GridView中编辑删除数据行,其中数据来自数据库 - how to edit delete data row in gridview where data come from database using binding function in asp.net c# 将新行数据添加到gridview asp.net c# - Add new row data to gridview asp.net c# ASP.Net如何从gridview C#更新数据库 - ASP.Net How to Update a database from a gridview C# 如何从数据库驱动的数据在C#和asp.net中设置GridView的字段宽度 - How do i set the field widths of a GridView in C# and asp.net from database driven data 使用ASP.NET C#绑定数据时如何使用数据库刷新GridView - How to refresh GridView with database when it is bound data using asp.net c# 如何在asp.net c#中将json数据绑定到gridview? - How to bind json data to gridview in asp.net c#? 如何在页面加载时以编程方式从GridView1的第一行第4列单元格中选择数据并将其保存到C#asp.net中的变量 - How to programatically select the data from GridView1's first row 4th column cell on page load and save it to a variable in C# asp.net 我如何 select 并将数据行保存在表格中? C# ASP.NET MVC 5 - How can I select and save data row in a table? C# ASP.NET MVC 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM