What is DataKeyNames in GridView C#?

DataKeyNames is the property of GridView which allows us to set the names of the Column Fields that we want to use in code but do not want to display it. Example Primary Keys, ID fields, etc.

What is GridView in asp net with example?

ASPX) Gridview is a control in asp.net, that displays the values of a data source(SQL server database) in a tabular form where each column represents a field and each row represents a record. The GridView control also enables you to select, sort, and edit these items.

What is the use of datakeynames in GridView?

When the DataKeyNames property is set, the GridView control automatically populates its DataKeys collection with the values from the specified field or fields, which provides a convenient way to access the primary keys of each row. The GridView control stores these key field values in the control state.

How to specify two key fields in a GridView element in markup?

In the example, the DataKeyNames attribute of the GridView element in markup specifies two key fields by using a comma to separate the names. To run this example, create a Web site that has the following:

How to set the value of column fields in datakeynames?

The values Column Fields which are set in DataKeyNames are available in code in DataKeys object which saves it in an Array called as DataKeyArray. Using DataKeyNames and DataKeys is fairly simple, you just need to set the name of the Column in DataKeyNames property as shown below. Here CustomerId is the name of the Column.

How do I get the value of a column in GridView?

Inside the Button click event handler, first the GridView Row is determined using the NamingContainer property and then finally the Row Index is determined. Using the Row Index, the DataKeys array is accessed and the value of the Column is fetched.