asp.net - Gridview fields values does not change on update -
i have gridview , want modify fields on gridview.this fields value not change when click on update button.i tried use postback control problem keep going.how can solve problem?
aspx code
<asp:gridview id="gview" runat="server" autogeneratecolumns="false" enablemodelvalidation="true" gridlines="horizontal" onrowdatabound="gview_rowdatabound" onrowediting="gview_rowediting" onrowupdating="gview_rowupdating" onrowcancelingedit="gview_rowcancelingedit"> <columns> <asp:boundfield datafield="subcategoryid" headertext="id" insertvisible="false" readonly="true" sortexpression="subcategoryid" /> <asp:templatefield headertext="category"> <itemtemplate> <asp:label id="lblcategory" runat="server"></asp:label> </itemtemplate> <edititemtemplate> <asp:dropdownlist id="ddlcategory" datavaluefield="categoryid" datatextfield="categoryname" runat="server" /> </edititemtemplate> </asp:templatefield> <asp:boundfield datafield="categoryname" headertext="category name" sortexpression="categoryname" /> <asp:commandfield buttontype="link" edittext="edit" headertext="edit" showeditbutton="true" showheader="false" canceltext="cancel" updatetext="update" /> </columns> </asp:gridview>
c# code
protected void gview_rowdatabound(object sender, gridviewroweventargs e) { if (e.row.rowtype == datacontrolrowtype.datarow) { label lbl = e.row.findcontrol("lblcategory") label; dropdownlist ddl = e.row.findcontrol("ddlcategory") dropdownlist; if ((e.row.rowstate & datacontrolrowstate.edit) > 0) { ddl.datasource = loadcategories(); ddl.databind(); } if (lbl != null) { lbl.text = getcategoryname(convert.toint32(gview.datakeys[e.row.rowindex][0])); } } } protected void gview_rowediting(object sender, gridviewediteventargs e) { gview.editindex = e.neweditindex; subcategoryload(); } protected void gview_rowupdating(object sender, gridviewupdateeventargs e) { int categoryid, subcategoryid; string categoryname; dropdownlist ddl = (dropdownlist)gview.rows[e.rowindex].findcontrol("ddlcategory"); subcategoryid = int.parse(gview.rows[e.rowindex].cells[0].text); categoryid = int.parse(ddl.selectedvalue); categoryname = gview.rows[e.rowindex].cells[2].text; gview.editindex = -1; updatesubcategory(subcategoryid,categoryname,categoryid); subcategoryload(); } public void subcategoryload() { using (sqlconnection conn = new sqlconnection(database.conn)) { conn.open(); string query = "select * dbo.subcategories"; using (sqldataadapter da = new sqldataadapter(query, conn)) { datatable dt = new datatable(); da.fill(dt); gview.datasource = dt; gview.databind(); } } } public int updatesubcategory(int subcategoryid, string categoryname, int categoryid) { using (sqlconnection conn = new sqlconnection(database.conn)) { conn.open(); string query = "update dbo.subcategories set categoryid = @categoryid, categoryname = @categoryname subcategoryid = @id"; using (sqlcommand cmd = new sqlcommand(query, conn)) { cmd.parameters.addwithvalue("@id", subcategoryid); cmd.parameters.addwithvalue("@categoryname", categoryname); cmd.parameters.addwithvalue("@categoryid", categoryid); return (int)cmd.executenonquery(); } } }
please check have bind gridview inside
if(!page.ispostback) { }
Comments
Post a Comment