本文介绍了ASP.Net如何从gridview更新数据库C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 如何从gridview更新SQL数据库? (更新记录后) 使用 - SharePoint - >应用程序页面 - 我知道,SharePoint ....使基本功能更基本和更难不可能(甚至没有一个SQLDatasource控件的一面,你必须自己添加它,如果你想它 - 不知道他们不想让你添加到项目或东西...) 我有数据显示在gridview ,并启用 AutoGenerateEditButton =True 。 Gridview的数据源是我从数据库填充的数据集。 我认为没有SQLDatasource绑定到gridview,你将不得不重新连接到数据库并手动更新数据库。 但我不能找到任何办法,除了SQLDataSource绑定到gridview,自动更新自动执行此操作。 方法但需要绑定到gridview的SQLDatasource 方法Nr1 方法Nr2 如何在编辑gridview后更新数据库? - 更新解决方案 - 添加了几个修改。感谢 protected void Page_Load(object sender,EventArgs e) { int employeerId = 1; //将所有员工添加到列表以查看他们。 string query =select emp.Name as [Employee Name],em。* from EthicsManagement em join EmployeeTable emp on em.employeeId = emp.employeeId其中emp.managerId =+ employeerId; DataSet ds = dbConn.returnSqlDataset(query); grdViewDetails.DataSource = ds; grdViewDetails.DataBind(); int x = grdViewDetails.Columns.Count; //隐藏列。 grdViewDetails.DataBind(); if(grdViewDetails.Columns.Count> 0) { grdViewDetails.Columns [2] .Visible = false; grdViewDetails.Columns [3] .Visible = false; } else { grdViewDetails.HeaderRow.Cells [0] .Visible = false; foreach(GridViewRow gvr in grdViewDetails.Rows) { gvr.Cells [2] .Visible = false; gvr.Cells [3] .Visible = false; } } } protected void grdViewDetails_RowEditing1(object sender,GridViewEditEventArgs e) { string query =选择emp.Name为[Employee Name],em。*从EthicsManagement em中连接EmployeeTable emp on em.employeeId = emp.employeeId其中emp.managerId =+ 1; grdViewDetails.EditIndex = e.NewEditIndex; //e.newedit index: - 将提供为其选择编辑按钮的行的索引 grdViewDetails.DataSource = dbConn.returnSqlDataset(query); grdViewDetails.DataBind(); } protected void grdViewDetails_RowUpdating(object sender,GridViewUpdateEventArgs e) { string query =select emp.Name as [员工姓名],em。*从EthicsManagement加入EmployeeTable emp on em.employeeId = emp.employeeId其中emp.managerId =+ 1; foreach(grdViewDetails.Rows [e.RowIndex] .Cells中的TableCell单元格) { if(cell.Controls [0]是TextBox) { TextBox textbox =(TextBox)cell.Controls [0]; string value = textbox.Text; } else { if(cell.Controls [0]是CheckBox) { CheckBox chkBoxWeek =(CheckBox)cell.Controls [0 ]; Boolean checkStatus = chkBoxWeek.Checked; } } } 解决方案> 行编辑:当用户点击编辑按钮后退后,将执行网格视图的行编辑事件。此事件将提供行索引 protected void gremp_rowediting(object sender,EventArgs e) { dataset ds =(dstaset)session [ds1]; gvemp.editindex = e.new editindex; //e.newedit index: - 将提供为其选择编辑按钮的行的索引 gvemp.Datasource = Ds.Table [employee]; gvemp.Databind(); } 行更新:这将执行回发,行更新将执行网格视图的事件过程 protected void gvemp-rowupdating(Object sender,EvenArgs e) { Textbox txt =(Textbox)gvemp.Rows [e.RowIndex] .cells [3] .controls [0]; //这里我将更新网格视图中的第三个单元格数据 int avg = int.parse(txt.Text); Dataset ds =(dataset)session [ds1]; ds.Tables [employee]。rows [e.Rowindex] [Average] = avg; ds.Tables [employee]。AcceptChanges(); session [Ds1] = ds; //它将覆盖Dataset的会话 //重新排列Gridview gvemp.editIndex = -1; gvemp.Datasource = Ds.Tables [employee]; gvemp.DataBind(); } 请点击此链接了解详情: http:/ /www.aspdotnet-sharepoint.com/2011/11/row-editdeleteupdate-in-grid-view-using.html How would you update a SQL database from a gridview? (after a record has been updated)Using - SharePoint -> Application page - I know, SharePoint...."Making the basic things more basic and the hard things Impossible" (There's not even a SQLDatasource control on the side, you have to add it yourself if you want it - don't know if they don't want you to add one to the project or something...)I have data being displayed in the gridview, and enabled the AutoGenerateEditButton="True" in the gridview. And the gridview's datasource is a dataset I populated from my database.I Take it that without a SQLDatasource thats bound to your gridview, you will have to connect to the database again and update the database manually. But I cant seem to find any way to do this other than the SQLDataSource that is bound to the gridview that does the updating automatically.Ways to do this but Requires the SQLDatasource bound to the gridviewMethod Nr1Method Nr2How will you go about updating the database after editing the gridview?--Update Solution-- Added a few modifications. Thanks protected void Page_Load(object sender, EventArgs e) { int employeerId = 1; //Add all the employees to the list to view them. string query = "select emp.Name as [Employee Name], em.* from EthicsManagement em join EmployeeTable emp on em.employeeId = emp.employeeId where emp.managerId =" + employeerId; DataSet ds = dbConn.returnSqlDataset(query); grdViewDetails.DataSource = ds; grdViewDetails.DataBind(); int x = grdViewDetails.Columns.Count; //Hide the columns. grdViewDetails.DataBind(); if (grdViewDetails.Columns.Count > 0) { grdViewDetails.Columns[2].Visible = false; grdViewDetails.Columns[3].Visible = false; } else { grdViewDetails.HeaderRow.Cells[0].Visible = false; foreach (GridViewRow gvr in grdViewDetails.Rows) { gvr.Cells[2].Visible = false; gvr.Cells[3].Visible = false; } } }protected void grdViewDetails_RowEditing1(object sender, GridViewEditEventArgs e) { string query = "select emp.Name as [Employee Name], em.* from EthicsManagement em join EmployeeTable emp on em.employeeId = emp.employeeId where emp.managerId =" + 1; grdViewDetails.EditIndex = e.NewEditIndex; //e.newedit index:- will be provide index of row for which edit button is selected grdViewDetails.DataSource = dbConn.returnSqlDataset(query); grdViewDetails.DataBind(); } protected void grdViewDetails_RowUpdating(object sender, GridViewUpdateEventArgs e) { string query = "select emp.Name as [Employee Name], em.* from EthicsManagement em join EmployeeTable emp on em.employeeId = emp.employeeId where emp.managerId =" + 1; foreach (TableCell cell in grdViewDetails.Rows[e.RowIndex].Cells) { if (cell.Controls[0] is TextBox) { TextBox textbox = (TextBox)cell.Controls[0]; string value = textbox.Text; } else { if (cell.Controls[0] is CheckBox) { CheckBox chkBoxWeek = (CheckBox)cell.Controls[0]; Boolean checkStatus = chkBoxWeek.Checked; } } } 解决方案 Row Editing:When user clicks on edit button post backtakes place,row editing event of grid view will be executed.This event will provide row indexprotected void gremp_rowediting(object sender,EventArgs e) { dataset ds=(dstaset)session["ds1"]; gvemp.editindex=e.new editindex;//e.newedit index:- will be provide index of row for which edit button is selected gvemp.Datasource=Ds.Table["employee"]; gvemp.Databind(); }Row Updating:This will perform post back,row-updating event procedure of grid view will be executedprotected void gvemp-rowupdating(Object sender,EvenArgs e) { Textbox txt=(Textbox)gvemp.Rows[e.RowIndex].cells[3].controls[0]; //here i will update the third cell data in grid view int avg=int.parse(txt.Text); Dataset ds=(dataset)session["ds1"]; ds.Tables["employee"].rows[e.Rowindex]["Average"]=avg; ds.Tables["employee"].AcceptChanges(); session["Ds1"]=ds;//it will overwrite the session of Dataset//Rearrange Gridview gvemp.editIndex=-1; gvemp.Datasource=Ds.Tables["employee"]; gvemp.DataBind(); }Follow this link for more details:http://www.aspdotnet-sharepoint.com/2011/11/row-editdeleteupdate-in-grid-view-using.html 这篇关于ASP.Net如何从gridview更新数据库C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-22 20:22