<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false" cellpadding = "4"
Forecolor = "#333333" gridlines = "NONE" onrowdeleting = "gridview1_rowdeleting" onrowediting = "gridview1_rowediting"
Onrowupdating = "gridview1_rowupdating" onrowcancelingedit = "gridview1_rowcancelingedit">
<Footerstyle backcolor = "#990000" font-bold = "true" forecolor = "white"/>
<Columns>
<Asp: boundfield datafield = "ID" headertext = "User ID" readonly = "true"/>
<Asp: boundfield datafield = "authorname" headertext = "User Name"/>
<Asp: boundfield datafield = "email" headertext = "email"/>
<Asp: boundfield datafield = "Age" headertext = "Age"/>
<Asp: commandfield headertext = "select" showselectbutton = "true"/>
<Asp: commandfield headertext = "edit" showeditbutton = "true"/>
<Asp: commandfield headertext = "delete" showdeletebutton = "true"/>
</Columns>
</ASP: gridview>
. CS
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
BIND ();
}
}
Protected void gridviewinclurowediting (Object sender, gridviewediteventargs E)
{
Gridview1.editindex = E. neweditindex;
BIND ();
}
// Delete
Protected void gridview1_rowdeleting (Object sender, gridviewdeleteeventargs E)
{
// String sqlstr = "delete from table where id = '" + gridview1.datakeys [E. rowindex]. value. tostring () + "'";
// Sqlcon = new sqlconnection (strcon );
// Sqlcom = new sqlcommand (sqlstr, sqlcon );
// Sqlcon. open ();
// Sqlcom. executenonquery ();
// Sqlcon. Close ();
BIND ();
}
// Update
Protected void gridview1_rowupdating (Object sender, gridviewupdateeventargs E)
{
// Sqlcon = new sqlconnection (strcon );
// String sqlstr = "Update table set field 1 = '"
// + (Textbox) (gridview1.rows [E. rowindex]. cells [1]. controls [0]). text. tostring (). trim () + "', Field 2 = '"
// + (Textbox) (gridview1.rows [E. rowindex]. cells [2]. controls [0]). text. tostring (). trim () + "', Field 3 = '"
// + (Textbox) (gridview1.rows [E. rowindex]. cells [3]. controls [0]). text. tostring (). trim () + "'where id = '"
// + Gridview1.datakeys [E. rowindex]. value. tostring () + "'";
// Sqlcom = new sqlcommand (sqlstr, sqlcon );
// Sqlcon. open ();
// Sqlcom. executenonquery ();
// Sqlcon. Close ();
Gridview1.editindex =-1;
BIND ();
}
// Cancel
Protected void gridview1_rowcancelingedit (Object sender, gridviewcancelediteventargs E)
{
Gridview1.editindex =-1;
BIND ();
}
// Bind
Public void BIND ()
{
String sqlstr = "select * From userinfo ";
// Sqlcon = new sqlconnection (strcon );
// Sqldataadapter myda = new sqldataadapter (sqlstr, sqlcon );
// Dataset myds = new dataset ();
// Sqlcon. open ();
// Myda. Fill (myds, "table ");
// Dataset myds = Zh. dbutility. sqlhelper. Query (sqlstr );
Gridview1.datasource = myds;
Gridview1.datakeynames = new string [] {"ID"}; // primary key
Gridview1.databind ();
// Sqlcon. Close ();
}