DataKeyNames is a property to define Read-only primary key like fields in a GridView control. We can also add some more fields to this property separated by commas.
For example, if 'CusCode' is the primary key and you want 'CusType' as a readonly field, you can assign the GridView control's DataKeyNames property as CusCode,CusType.
To retreive the value from the GridView control in RowDataBound event or any other event of the GridView control, use the below syntax.
To take Field 1, use GridView1.DataKeys[e.Row.RowIndex][0].ToString()
To take Field 2, use GridView1.DataKeys[e.Row.RowIndex][1].ToString()
To take Field n, use GridView1.DataKeys[e.Row.RowIndex][n-1].ToString()
Happy Codings..
|