M
Mike P
When I am performing a command on a row in my gridview, it works fine,
unless I have paging enabled and I try t execute the command on a page
other than the first page. The error I get is 'Index was out of range.
Must be non-negative and less than the size of the collection' on the
capitalised line in my code :
protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "ActivateDeactivate")
{
// 1)Convert the row index stored in the CommandArgument
property to an Integer
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked by the
user from the Rows collection
GridViewRow row = GridView1.Rows[index];
//get datakeys
INT ID = (INT)GRIDVIEW1.DATAKEYS[ROW.DATAITEMINDEX].VALUE;
//2)get current status of checkbox and switch
bool blnActivate = false;
CheckBox cb = new CheckBox();
cb =
(CheckBox)GridView1.Rows[index].FindControl("chkActive");
if (cb.Checked.ToString() == "True")
{
blnActivate = true;
}
else
{
blnActivate = false;
}
//3)update status
DataAccess da = new DataAccess();
//not doing anything with DBResult here
DBResult dbrUpdateStatus =
(Xerox.DBResult)da.ActivateDeactivateUser(id, blnActivate);
GridView1.EditIndex = -1;
//refresh gridview
GridView1.DataBind();
}
}
Any advice would be appreciated.
unless I have paging enabled and I try t execute the command on a page
other than the first page. The error I get is 'Index was out of range.
Must be non-negative and less than the size of the collection' on the
capitalised line in my code :
protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "ActivateDeactivate")
{
// 1)Convert the row index stored in the CommandArgument
property to an Integer
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked by the
user from the Rows collection
GridViewRow row = GridView1.Rows[index];
//get datakeys
INT ID = (INT)GRIDVIEW1.DATAKEYS[ROW.DATAITEMINDEX].VALUE;
//2)get current status of checkbox and switch
bool blnActivate = false;
CheckBox cb = new CheckBox();
cb =
(CheckBox)GridView1.Rows[index].FindControl("chkActive");
if (cb.Checked.ToString() == "True")
{
blnActivate = true;
}
else
{
blnActivate = false;
}
//3)update status
DataAccess da = new DataAccess();
//not doing anything with DBResult here
DBResult dbrUpdateStatus =
(Xerox.DBResult)da.ActivateDeactivateUser(id, blnActivate);
GridView1.EditIndex = -1;
//refresh gridview
GridView1.DataBind();
}
}
Any advice would be appreciated.