Today I was adding an Export to Excel” op­tion to a Gridview. I wanted to mark each ex­ported record in the data­base, but it was­n’t im­me­di­ately ap­par­ent how I would do so. After far more search­ing than I ex­pected, I came up with the fol­low­ing so­lu­tion:

foreach (DataKey dataKey in myGridView.DataKeys)

I was think­ing I would need LINQ and cast­ing and such…and it turns out you can just loop through a Gridview’s data keys.