ASP.NET GridView Editing

The GridView control provides many built-in capabilities that allow the user to sort, update, delete, select, and page through items in the control.

gridview-edit

The GridView control facilitates convenient row-by-row editing functionality, enabling users to modify data directly within the control. An editable GridView incorporates an additional column in each row, housing an Edit button. When a user clicks on the Edit button, the corresponding row transitions into an editable state. As a result, the Edit button transforms into Update and Cancel buttons, while the remaining columns become TextBoxes, allowing for easy data manipulation.

Enable editing capabilities

To enable editing capabilities, you can set the AutoGenerateEditButton property of the GridView control to true. This setting triggers the automatic generation of the Edit button within each row, streamlining the editing process for users. Additionally, it is crucial to specify the DataKeyNames property with the primary key of the underlying table. By doing so, the GridView control can track and identify individual rows accurately, ensuring the proper updating of data when the user clicks the Update button.

Enabling editing functionality within the GridView control empowers users to modify column values seamlessly, enhancing the interactivity and flexibility of the displayed data. Users can effortlessly update one or more fields within a row and then save their changes by clicking the Update button, providing a convenient means of data manipulation.

Download Database

In this article I have used Microsoft's Pubs database for sample data. You can download it free from the following link.

Download

Before you start to create Edit functionality on GridView in your asp file, you should create a ConnectionString in your web.Config File. Double click the web.config file on the right hand side of the Visual Studio and add the following connectionstring code in that file.

web.config file

Web.Config File

<?xml version="1.0"?> <configuration> <connectionStrings> <add name="SQLDbConnection" connectionString="Server=Your-Server-Name; Database=pubs; User Id=sa; password= your-passoword" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration>

The following program shows how to update gridview values from an ASP.NET application

Default.aspx

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AutoGenerateEditButton="true" AllowSorting="True" DataSourceID="SqlDataSource1" AllowPaging="True" DataKeyNames="stor_id"> <Columns> <asp:BoundField ReadOnly="True" HeaderText="stor_id" DataField="stor_id" SortExpression="stor_id"></asp:BoundField> <asp:BoundField HeaderText="stor_name" DataField="stor_name" SortExpression="stor_name"></asp:BoundField> <asp:BoundField HeaderText="stor_address" DataField="stor_address" SortExpression="stor_address"></asp:BoundField> <asp:BoundField HeaderText="city" DataField="city" SortExpression="city"></asp:BoundField> <asp:BoundField HeaderText="state" DataField="state" SortExpression="state"></asp:BoundField> <asp:BoundField HeaderText="zip" DataField="zip" SortExpression="zip"></asp:BoundField> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SQLDbConnection %>" SelectCommand="select * from stores" UpdateCommand="UPDATE [stores] SET [stor_name] = @stor_name , [stor_address] = @stor_address , [city] = @city , [state]=@state , [zip]=@zip WHERE [stor_id] = @stor_id" > <UpdateParameters> <asp:Parameter Type="String" Name="stor_name"></asp:Parameter> <asp:Parameter Type="String" Name="stor_address"></asp:Parameter> <asp:Parameter Type="String" Name="city"></asp:Parameter> <asp:Parameter Type="String" Name="state"></asp:Parameter> <asp:Parameter Type="String" Name="zip"></asp:Parameter> </UpdateParameters> </asp:SqlDataSource> </div> </form> </body> </html>