The above piece of code will also do the same as label in Item Template and Textbox in the Edit Item Template.
In data driven web sites, users are normally allowed to view data and administrators are allowed to add, edit or delete data.
In this tutorial I will show you how you can create an editable Grid View control for administrators to modify database records directly into the Grid View control.
Editing the Grid View control can be an incredibly easy feature to add for certain database tables, especially those who have no foreign key associated and can easily be edited through the use of a standard Text Box web control as shown in the figure below: Before we start examining the ASP.
NET Grid View markup and associated code lets first enumerate the important code pieces of this tutorial.
In the Page_Load event, you simply load your data from the database and bind it with the Grid View control as shown below: For brevity I am not storing connection string in the web.config and also not using any Data Access Component.
Next event is the Grid View Row Editing event to give ASP. You need to set the Edit Index property of the Grid View with the New Edit Index property of the Grid View Edit Event Args class.
If user clicks the Cancel button while Grid View is in editing mode, you need to Cancel the Editing process and also need to reset the Edit Index property to -1 in the Row Canceling Edit event of the Grid View control as shown below: The last step is to handle the Row Updating event to actually update the back end database.
To do this we first need to obtain the reference of the current row by using its index no as shown in the first line below.
You can see the C# code below will give the row number whatever you are editing by New Edit Index property.
So Grid View will put the appropriate row into the Edit Mode by assigning row number to its Edit Index property. You can follow any responses to this entry through the RSS 2.0 feed.
Last line of the following code is calling a local method for updating the database and passing all the parameters to this method.