gridview edit problems when paging

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.
 
S

S. Justin Gengo

Mike,

You'll have to make certain that your grid has been re-bound and set to the
same page before you can get the datakey. I suspect that at the point in
question in your code your grid is either not bound again yet and therefore
doesn't have any rows at all or that it is on the wrong page and the proper
row is not being found. You can check this by putting in lines to get the
grid's rowcount and current page just before the line of code that is
erroring out and then setting a breakpoint on the line of code producing the
error and seeing what the grid is set to just before it fires.

Regards,

--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
M

Mike P

Justin,

I am getting a row count of 10 on page 2, but a DataItemIndex of 15 (it
is the 15th row altogether including all pages, that I am selecting).
Is this what is causing the problem, and how do I prevent it?


Thanks,

Mike
 
M

Mike P

I've also noticed that even though I get DataItemIndex = 15, I get
DataItem is null.
 
Joined
May 1, 2007
Messages
1
Reaction score
0
I've also noticed that even though I get DataItemIndex = 15, I get
DataItem is null.

I am trying to solve that problem but could not be sıccesfull. If you solve the problem can yo inform me please???
 
Joined
Nov 18, 2009
Messages
1
Reaction score
0
Modify the following line:
INT ID = (INT)GRIDVIEW1.DATAKEYS[ROW.DATAITEMINDEX].VALUE;

To:
INT ID = (INT)GRIDVIEW1.DATAKEYS[ROW.DataItemIndex - (GRIDVIEW1.PageIndex * GRIDVIEW1.PageSize)].VALUE;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top