Friday 19 October 2012

ASP.NET - How to get gridview cells by cell name



The following line of code will get value form cell 12 in row 10
myDataGrid.Rows(10).Cells(12).Text

If I add new field (cell) to this datagrid, before cell 12, I will have to modify my code to
myDataGrid.Rows(10).Cells(13).Text

If there are many columns in the grid view, it might be very time consuming..

To get cells in datagrid by name,
1. Add column name to datakeynames 


Ensure it is the same is DataField in Fields screen
then replace 
myDataGrid.Rows(10).Cells(13).Text
with

myDataGrid.DataKeys(10).Values("id").ToString



No comments:

Post a Comment