How to access gridview editing row element value in a class function?
-
To access formview editing element in a class function, I can use the following code: HttpContext.Current.Request.Form("fvwName$elmName") How can I access gridview editing row element value in a class function?
+ Get reference to the Page instance by casting the
HttpContext.Current.Handler
property to thePage
. + Get reference to the GridView control using the Page instance and theFindControl
method. + Get reference to the editable row using the properties of the GridView instance that you have in the previous step. -
+ Get reference to the Page instance by casting the
HttpContext.Current.Handler
property to thePage
. + Get reference to the GridView control using the Page instance and theFindControl
method. + Get reference to the editable row using the properties of the GridView instance that you have in the previous step. -
Below is the sample code for getting the edit row, and once you have the reference you can easily access the edit values from the cells of the row:
public GridViewRow GetEditRow()
{
Page page = HttpContext.Current.Handler as Page;GridView gridView = page.FindControl("GridView1") as GridView; GridViewRow editRow = gridView.Rows\[gridView.EditIndex\]; return editRow; }
-
Below is the sample code for getting the edit row, and once you have the reference you can easily access the edit values from the cells of the row:
public GridViewRow GetEditRow()
{
Page page = HttpContext.Current.Handler as Page;GridView gridView = page.FindControl("GridView1") as GridView; GridViewRow editRow = gridView.Rows\[gridView.EditIndex\]; return editRow; }
-
When I try your code, I got the following error on line GridViewRow editRow = gridView.Rows[gridView.EditIndex]; Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
That's because I'm using the Edit built-in button in the GridView control so that when I hit the Edit button, the control's mode is changed to the Edit mode and the EditIndex property is set internally. And when the Update button is clicked, I can access the edit row in a seperate class with the above code. I'm not sure when you want to run that code, but if you use your own button, you can raise the Edit command like the built-in button by setting the CommandName as
Edit
. You might also try to have a look at the EditIndex value at runtime to diagnose your problem. The basic idea is that you need a way to determine the row that is being edited and the index of the row comes to mind as an option.