Asp.net data binding (editing data to delete data) example

Source: Internet
Author: User
The code is as follows: Copy code


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 ();
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.