Sunday, 31 January 2010

Always learning

Having worked for many years with ASP.NET GridViews I have never noticed before that when you use a ButtonField ( for instance) as a column in the GridView, when handling the event in the code-behind in the event created for this (mygrid_RowCommand(object sender, GridViewCommandArgument e) for instance) that e.CommandArgument is the offset of the row ON THAT PAGE!!! So if the gridview is paged at 20 per page, the command argument will be 0-19. So, when you are trying to recover the DataKeys for a paged gridview, ASP.NET has worked it all out for you so there is no need for code like:

gv.DataKeys[e.CommandArgument + (gv.PageIndex * gv.PageCount)]

all you really need is:

gv.DataKeys[int.Parse(e.Command.ToString())] - and job done!!!

As I say, always learning something!!!

No comments:

Post a Comment