c# - update the gridview using onrowupdating method -
i copied of part msdn. clicking on update nil
protected void gridview1_rowupdating(object sender, gridviewupdateeventargs e) { seek { datatable dt = (datatable)session["hospital"]; gridviewrow row = gridview1.rows[e.rowindex];//getting current index dt.rows[row.dataitemindex]["name"] = ((textbox)(row.cells[0].controls[0])).text; dt.rows[row.dataitemindex]["phone_no."] = ((textbox)(row.cells[1].controls[0])).text; dt.rows[row.dataitemindex]["address"] = ((textbox)(row.cells[2].controls[0])).text; dt.rows[row.dataitemindex]["hos_id"] = ((textbox)(row.cells[3].controls[0])).text; gridview1.editindex = -1; gvbind(gridview1, "select * hospital"); // } catch(oracleexception ex) { lblresult.text = ex.message.tostring(); } grab (nullreferenceexception nx) { lblresult.text = nx.message.tostring(); } }
jere function binds gridview
datasource
.
public void gvbind(gridview g, string sql) { seek { conn.open(); oraclecommand cmd = new oraclecommand(); cmd.connection = conn; cmd.commandtext = sql; cmd.commandtype = commandtype.text; // cmd.executenonquery(); oracledataadapter da = new oracledataadapter(cmd); dataset ds = new dataset(); da.fill(ds); session["hospital"] = ds.tables[0]; if (ds.tables[0].rows.count > 0) { g.datasource = ds; g.databind(); } else { ds.tables[0].rows.add(ds.tables[0].newrow()); g.datasource = ds; g.databind(); int columncount = g.rows[0].cells.count; g.rows[0].cells.clear(); g.rows[0].cells.add(new tablecell()); g.rows[0].cells[0].columnspan = columncount; g.rows[0].cells[0].text = "no records found"; } conn.close(); conn.dispose(); } }
here asp.net code < need add together edit template , itemtemplate utilize >
<asp:gridview id="gridview1" runat="server" datakeynames="hos_id" autogeneratecolumns="false" onrowdeleting="gridview1_rowdeleting" onpageindexchanging="gridview1_pageindexchanging" onrowupdating="gridview1_rowupdating" onrowediting="gridview1_rowediting" onrowcancelingedit="gridview1_rowcancelingedit"> <columns> <asp:boundfield datafield="name" headertext="name" /> <asp:boundfield datafield="phone_no." headertext="phone_no." /> <asp:boundfield datafield="address" headertext="address" /> <asp:boundfield datafield="hos_id" headertext="hos_id"/> <asp:commandfield showeditbutton="true" /> <asp:commandfield showdeletebutton="true" /> </columns> </asp:gridview>
the problem appears code fills datatable
values edited row in gridview1_rowupdating
discards them when gvbind
called. datatable
stored in session["hospital"]
re-create of info loaded database when grid lastly updated. changing values in changes in-memory copy, not underlying table.
instead, gridview1_rowupdating
should:
datatable
using dataadapter
. find row(s) in datatable
correspond updated rows in grid. copy new values in appropriate row , cell in datatable
. update grid datatable
. includes changes made others outside of page. write changes database. close , dispose of connection , related objects. c# asp.net database gridview
No comments:
Post a Comment