|
1. Edit data
Protected void gridviewinclurowediting (object sender, GridViewEditEventArgs e)
{
This. GridView1.EditIndex = e. NewEditIndex;
GetData ();
}
2. Cancel editing.
Protected void GridView1_RowCancelingEdit (object sender, GridViewCancelEditEventArgs e)
{
This. GridView1.EditIndex =-1; // cancel the editing status
GetData ();
}
3. Add fields to add the displayed fields in the DataKeyNames attribute.
4. Retrieve database data
Protected void GridView1_RowUpdating (object sender, GridViewUpdateEventArgs e)
{
// Modify Database Events
String Cus = this. GridView1.DataKeys [e. RowIndex] [0]. ToString (); // Data Index
String Companys = this. GridView1.DataKeys [e. RowIndex] [1]. ToString ();
Response. Write (Cus + "----" + Companys); // view the data retrieved
}
5. Retrieve the data submitted after the user changes
Protected void GridView1_RowUpdating (object sender, GridViewUpdateEventArgs e)
{
String Cus = this. GridView1.DataKeys [e. RowIndex] [0]. ToString ();
String Com = (TextBox) this. GridView1.Rows [e. RowIndex]. Cells [0]. Controls [0]). Text. ToString ();
String Con = (TextBox) this. GridView1.Rows [e. RowIndex]. Cells [1]. Controls [0]). Text. ToString ();
String Title = (TextBox) this. gridView1.Rows [e. rowIndex]. cells [2]. controls [0]). text. toString (); // Obtain (client) cell data;
Response. write (cuz + "----" + Com + "----" + Con + "----" + Title); // The two data records obtained in the database are assigned to Cuz and Com respectively;
}
6. Modify data
Protected void GridView1_RowUpdating (object sender, GridViewUpdateEventArgs e)
{
String Cus = this. GridView1.DataKeys [e. RowIndex] [0]. ToString ();
String Com = (TextBox) this. GridView1.Rows [e. RowIndex]. Cells [0]. Controls [0]). Text. ToString ();
String Con = (TextBox) this. GridView1.Rows [e. RowIndex]. Cells [1]. Controls [0]). Text. ToString ();
String Title = (TextBox) this. gridView1.Rows [e. rowIndex]. cells [2]. controls [0]). text. toString (); // Obtain (client) cell data;
Update (Cus, Com, Con, Title); // (call the Update method) to perform the modification operation
This. GridView1.EditIndex =-1;
GetData (); // rebind data
}
Public void Update (string Cus, string Com, string Con, string Title)
{
SqlConnection conn = new SqlConnection ("server = localhost; database = Northwind; User = sa; PassWord = ");
SqlCommand com = new SqlCommand ("Update MERS set CompanyName = '" + Com + "', ContactName = '" + Con + "', contactTitle = '"+ Title +" 'Where mermerid =' "+ Cus +" '", conn );
Conn. Open ();
Com. ExecuteNonQuery ();
Conn. Close ();
}
7. Delete data
Protected void GridView1_RowDeleting (object sender, GridViewDeleteEventArgs e)
{
String Cus = this. GridView1.DataKeys [e. RowIndex] [0]. ToString ();
Del (CUZ );
GetData (); // rebind data
}
Public void del (string Cus)
{
SqlConnection conn = new SqlConnection ("server = localhost; database = Northwind; User = sa; PassWord = ");
SqlCommand com = new SqlCommand ("Delete from MERS where CustomerID = '" + Cus + "'", conn );
Conn. Open ();
Com. ExecuteNonQuery (); // This method is bound because the data cannot be deleted before it is deleted.
Conn. Close ();
} |