http://www.aspdotnetcodes.com/GridView_Insert_Edit_Update_Delete.aspx
Simple Insert, Select, Edit, Update and Delete in Asp.Net GridView control
After the GridView swithes to Edit Mode, you can view the TextBoxes and DropDownlList controls along with Update and Cancel linkbuttons in the Edit mode. To cancel this action, add the following two lines of code in the GridView’s RowCancelingEdit event.
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
FillCustomerInGrid();
}
You can update the data to the customer table, by adding the following lines of code in the GridView’s RowUpdating event.
The above block of codes in RowUpdating event, finds the control in the GridView, takes those values in pass it to the CustomersCls class Update method. The first parameter GridView1.DataKeys[e.RowIndex].Values[0].ToString() will return the Code of the Customer. That is the unique code for each customer to perform update function.
Delete in GridView
To delete a row from the customer table, add the following lines of code in the GridView’s RowDeleting event. Here you have to pass the unique Code of customer which is in GridView1.DataKeys[e.RowIndex].Values[0].ToString() to the Delete method of the CustomersCls class.
This article gives you some basic idea of inserting data into database from a GridView control and does all database manipulations within the GridView without binding it with any Asp.Net Data source controls.
Output:
Simple Insert, Select, Edit, Update and Delete in Asp.Net GridView control
Introduction | ||||||||||||||||
This article explains the methods of binding Asp.Net GridView control with simple DataSet or DataTable, and also explains the methods like Insert, Edit, Update and Delete function in the GridView control. | ||||||||||||||||
You can see most of the articles and tutorials in many websites teach you the way to bind a GridView control to the database with some Data Source controls such as SQLDataSource, ObjectDataSource, AccessDataSource and even XMLDatasource. But this article focus on the other way, by binding the GridView control to the database with the help of simple DataTable and perform adding of new records to the database from the footer row of the GridView control. And on each row, we are going to manipulate the records by editing, updating, cancelling and deleting functions. Sample Scenario For demonstration, we are going to fill an ASP.NET GridView control with data from a database. Let us take a simple Customer Table. This customer table contains 6 columns such as Customer Unique Code, Name of the Customer, Gender, City, State and Customer Type. We are going to add new records to the database and populate it in the GridView control. Then record manipulation (edit, update and delete) will be done in each and every column with the server controls such as TextBox and DropDownList. In these 6 columns, we are not going to display Customer Code, and to edit Customer Name and City columns we are going to provide TextBox, to edit Gender and Customer Type we are going to use DropDownList. Additionally, the values for Gender DropDownList will be filled with static values such as Male and Female, other DropDownList for Customer Type, we will be filled dynamically with the values from the Database.
|
After the GridView swithes to Edit Mode, you can view the TextBoxes and DropDownlList controls along with Update and Cancel linkbuttons in the Edit mode. To cancel this action, add the following two lines of code in the GridView’s RowCancelingEdit event.
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
FillCustomerInGrid();
}
You can update the data to the customer table, by adding the following lines of code in the GridView’s RowUpdating event.
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { TextBox txtName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName"); DropDownList cmbGender = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("cmbGender"); TextBox txtCity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCity"); DropDownList cmbType = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("cmbType"); customer.Update(GridView1.DataKeys[e.RowIndex].Values[0].ToString(),txtName.Text, cmbGender.SelectedValue,txtCity.Text, cmbType.SelectedValue); GridView1.EditIndex = -1; FillCustomerInGrid(); } |
The above block of codes in RowUpdating event, finds the control in the GridView, takes those values in pass it to the CustomersCls class Update method. The first parameter GridView1.DataKeys[e.RowIndex].Values[0].ToString() will return the Code of the Customer. That is the unique code for each customer to perform update function.
Delete in GridView
To delete a row from the customer table, add the following lines of code in the GridView’s RowDeleting event. Here you have to pass the unique Code of customer which is in GridView1.DataKeys[e.RowIndex].Values[0].ToString() to the Delete method of the CustomersCls class.
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { customer.Delete(GridView1.DataKeys[e.RowIndex].Values[0].ToString()); FillCustomerInGrid(); } |
This article gives you some basic idea of inserting data into database from a GridView control and does all database manipulations within the GridView without binding it with any Asp.Net Data source controls.
Output:
Sample For Simple Insert, Select, Edit, Update and Delete in GridView | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This the sample page for making simple insert, select, edit, update and delete functions in GridView control. You can entered your own data in the controls and click Add New link to add into the GridView control. Then you can do Edit, Update and Delete functions. Even you can try by deleting all the existing records and insert new records from the controls placed in the footer section of the GridView control. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
No comments:
Post a Comment