This example shows how to edit and update records in a GridView in ASP.NET. This example is helpful in situations where an administrator needs to edit several records from the database. For demonstration, I have created a database (named Database.mdf) in which we have a table named tbl_Employee.
Table Schema used in this example
Let's Begin
- Drop a GridView Control from the toolbox and set AutoGenerateColumns to false.
- Add the Columns Collection (tag) that manages the collection of column fields.
- Add TemplateField inside the Columns Collection that is used to display custom content in a data-bound control.
- Add an ItemTemplate in the TemplateField that specifies the content to display for the items in a TemplateField.
- Add an EditItemTemplate in the TemplateField that specifies a custom user interface (UI) for the item in edit mode.
- Set the Command name property to Edit in the Edit button, Update in the Update button and Cancel in the Cancel Button depending on their respective Events.
- Add OnRowEditing, OnRowUpdating and OnRowCancelingEdit events to the GridView.
Default.aspx Code:
Default.aspx.cs Code
Final Preview:
![Edit and Update Record in GridView]()
I hope you like it. Thanks.