Click here to Skip to main content
15,666,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi i want edit gridview in code behind
how can i get fied ID

i just want to find DataKeyNames that user want to editing

Set the DataKeyField in GridView as

<asp:gridview id="gvList" runat="server" datakeynames="Id" xmlns:asp="#unknown">

Then you can get the key value for each column as:

for (int i = 0; i < gvList.Rows.Count; i++)
        Int32 key = Convert.ToInt32(gvList.DataKeys[i].Value.ToString());

Or, if you are working between any cell-event you can use the object of DataGridViewCellEventArgs as

Int32 key = Convert.ToInt32(gvList.DataKeys[e.RowIndex].Value);
Share this answer
int key = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
Share this answer
you can set multiple datakeynames by , separation.
and you can access this by GridView1.DataKeys[e.RowIndex].Values["ID"];
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900