may be limited learning ability, I from last night to today, accumulated up to a minimum of five or six hours, finally understand the GridView editor. After all, the code that was found is not commented and explained.
I want to record, but also hope that the new ding can be less detours.
<form id= "Form1" runat= "Server" > <div class= "div1" > <asp:gridview id= "GridView1" width= "100%" Au togeneratecolumns= "False" runat= "Server" datakeynames= "Sid" onrowediting= "gridview1_rowediting" onrowupdating= "Gridview1_rowupdating" onrowcancelingedit= "Gridview1_rowcancelingedit" backcolor= "white" BorderColor= "#DEDFDE" Borderstyle= "None" borderwidth= "1px" cellpadding= "4" forecolor= "Black" gridlines= "Vertical" > <alternating RowStyle backcolor= "white"/> <Columns> <asp:boundfield datafield= "Sid" headertext= "study number"/ > <asp:boundfield datafield= "Sname" headertext= "name"/> <asp:boundfield D Atafield= "Sage" headertext= "Age"/> <asp:boundfield datafield= "Sphone" headertext= "Phone"/> <asp:boundfield datafield= "spwd" headertext= "password"/> <asp:commandfield showdeletebutton= "true" Head ertext= "Operation"/> <asp:commandfield showeditbutton= "true" headertext= "operation"/> </Columns> <fo Oterstyle backcolor= "#CCCC99"/>
protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {binddata (); }} private void Binddata () {String sql = string. Format ("SELECT * from Student"); DataTable dt = sqlhelper.exctutedatatable (SQL); Gridview1.datasource = DT; Gridview1.gridlines = Gridlines.both; GridView1.RowStyle.HorizontalAlign = horizontalalign.center;//cell text centered gridview1.databind (); GridView1.EditRowStyle.ForeColor = color.red; } protected void Gridview1_rowediting (object sender, GridViewEditEventArgs e) {Gridview1.editind ex = E.neweditindex; Binddata (); } protected void Gridview1_rowupdating (object sender, Gridviewupdateeventargs e) {/* First obtains the No. 0 of the row to be edited The first child control in the cell, go to the TextBox type and then take the value */int Sid = Int. Parse ((TextBox) Gridview1.rows[e.rowindex]. Cells[0]. ContrOls[0]). Text.tostring (). Trim ()); The/*findcontrol parameter fills in the ID of the control, but we need to get this ID first, so this usage is not recommended. *///string Sname = (Gridview1.rows[e.rowindex]. Cells[0]. FindControl ("Sname") as TextBox). Text; String Sname = ((TextBox) Gridview1.rows[e.rowindex]. CELLS[1]. Controls[0]). Text.tostring (). Trim (); String Sage = ((TextBox) Gridview1.rows[e.rowindex]. CELLS[2]. Controls[0]). Text.tostring (). Trim (); String sphone = ((TextBox) Gridview1.rows[e.rowindex]. CELLS[3]. Controls[0]). Text.tostring (). Trim (); String spwd = ((TextBox) Gridview1.rows[e.rowindex]. CELLS[4]. Controls[0]). Text.tostring (). Trim (); String sql = string. Format ("Update Student set Sid={0},sname= ' {1} ', Sage= ' {2} ', Sphone= ' {3} ', Spwd= ' {4} ' where sid={5} ', Sid, Snam E, Sage, Sphone, spwd, Int. Parse (Gridview1.datakeys[e.rowindex]. Value.tostring ())); Sqlhelper.executenonquery (SQL); } protected void Gridview1_rowcancelingedit (OBJECT sender, Gridviewcancelediteventargs e) {gridview1.editindex =-1; Binddata (); }
The GridView enters the editing state, the novice looks.