problem. SelectedIndex returns a zero-based index number that represents where the row occurs
the grid. This isn’t the information you need to insert into the query that gets the related records.
Instead, you need a unique key field from the corresponding row. For example, if you have a
products, you need to be able to get the ProductID for the selected row. In order to get this
you need to tell the GridView to keep track of the key field values.
The way you do this is by setting the DataKeyNames property for the GridView. This property
requires a comma-separated list of one or more key fields. Each name you supply must match
the fields in the bound data source. Usually, you’ll have only one key field. Here’s an example
the GridView to keep track of the CategoryID values in a list of product categories:
Once you’ve established this link, the GridView is nice enough to keep track of the key fields
for the selected record. It allows you to retrieve this information at any time through the
Often, a database search will return too many rows to be realistically displayed in a single page.
client is using a slow connection, an extremely large GridView can take a frustrating amount of
to arrive. Once the data is retrieved, the user may find out it doesn’t contain the right content
or that the search was too broad and they can’t easily wade through all the results to find the
The GridView handles this scenario with an automatic paging feature. When you use automatic
paging, the full results are retrieved from the data source and placed into a DataSet. Once the
is bound to the GridView, however, the data is subdivided into smaller groupings (for example,
rows each), and only a single batch is sent to the user. The other groups are abandoned when the
finishes processing. When the user moves to the next page, the same process is repeated—in
words, the full query is performed once again. The GridView extracts just one group of rows,
page is rendered.
To allow the user to skip from one page to another, the GridView displays a group of pager
controls at the bottom of the grid. These pager controls could be previous/next links (often
< and >) or number links (1, 2, 3, 4, 5, . . . ) that lead to specific pages. If you’ve ever used a
engine, you’ve seen paging at wo