Datakeys in gridview rowupdating
Please keep in mind that there are many different ways to update the database using the Grid View control depending on the type of data you are updating and the functionality the user need and there are many solutions to achieve the same end result.In the above tutorial I tried to give you one of the easiest solutions and I hope you can go explore Grid View control editing further after learning the basics from this tutorial. A datakey represents the primary key field or field of a record that can uniquely identify it in a data-bound control. Row Index represents the index of the particular row.Many a times in our applications we use primary keys to get control of the particular record in the gridview. 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.By using the Grid View Row object we can find the reference of any control in the row in which user is editing the data.For this situation, many developers use invisible columns (Hidden columns) in the gridview to identify that row. We can achieve the same functionality using Datakeys.
I am using Template Field’s Item Template and Edit Item Template for providing read only and editing interface of the same column in the Grid View.This collection only requires the row index no and it will give us the primary key.Last line of the following code is calling a local method for updating the database and passing all the parameters to this method.The visitors to a web site can typically be divided into two groups: the users and the administrators.In data driven web sites, users are normally allowed to view data and administrators are allowed to add, edit or delete data.
In this example, I am going to retrieve values from categories table of Northwind database. Drag a Grid View into the Web Form and assign Data Key Names property with the primary key column i.e. In the gdview_Row Editing and gdview_Row Deleting event, we have used the below code to retrieve categoryid from datakey of Grid View, int catid = int.