Private Void BIND ()
{
Sqlconnection con = New Sqlconnection (configurationmanager. connectionstrings [ " Connstr " ]. Connectionstring );
Sqldataadapter SDA = New Sqldataadapter ( " Select * form name " , Con );
Dataset DS = New Dataset ();
SDA. Fill (DS, " Temp " );
Con. Close ();
Gridview1.datasource = DS. Tables [ " Temp " ]. Defaultview;
Gridview1.databind ();
}
Private Void Fill ( Int ID, String Name, Int Age)
{
Sqlconnection con = New Sqlconnection (configurationmanager. connectionstrings
[ " Connstr " ]. Connectionstring );
Sqldataadapter SDA = New Sqldataadapter ( " Select * from name " , Con );
Sqlcommandbuilder scbld = New Sqlcommandbuilder (SDA );
// If you do not see the above sentence, then dataset will be able to update only selete
Dataset DS = New Dataset ();
Try
{
SDA. Fill (DS, " Temp " );
DS. Tables [ " Temp " ]. Defaultview. Sort = " ID " ;
// Sort by ID
Int Index = DS. Tables [ " Temp " ]. Defaultview. Find (ID );
// Locate the index of the row where the data we want is located
DS. Tables [ " Temp " ]. Rows [Index] [ " Name " ] = Name;
DS. Tables [ " Temp " ]. Rows [Index] [ " Age " ] = Age;
// The data in dataset must be updated using arrays.
Int Rows = SDA. Update (DS, " Temp " );
Response. Write ( " Updated successfully " + Rows + " Row data " );
}
Catch (Exception E)
{
Response. Write ("The cause of the error is:" +E. Message );
}
}
Protected Void Gridview1_rowupdating ( Object Sender, gridviewupdateeventargs E)
{
Int Index = E. rowindex;
Int ID = Convert. toint32 (gridview1.rows [Index]. cells [ 1 ]. Text );
// The above indicates that the row number of gridview1 needs to be dynamically retrieved, while the column number is fixed.
String Name = (Textbox) gridview1.rows [Index]. cells [ 2 ]. Findcontrol ( " Textbox1 " ). Text;
Int Age = Convert. toint32 (textbox) gridview1.rows [Index]. cells [ 3 ]. Findcontrol ( " Textbox2 " ). Text );
Fill (ID, name, age );
Gridview1.editindex = - 1 ;
BIND ();
}