473,394 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

gridview edit problems when paging

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.

*** Sent via Developersdex http://www.developersdex.com ***
Sep 15 '06 #1
3 3171
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
"Mike P" <mi*******@gmail.comwrote in message
news:e8***************@TK2MSFTNGP06.phx.gbl...
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.

*** Sent via Developersdex http://www.developersdex.com ***

Sep 15 '06 #2
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
*** Sent via Developersdex http://www.developersdex.com ***
Sep 15 '06 #3
I've also noticed that even though I get DataItemIndex = 15, I get
DataItem is null.

*** Sent via Developersdex http://www.developersdex.com ***
Sep 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: ck388 | last post by:
For some reason when I enable the callback feature of the gridview I still get a page refresh, that is it seems like there is a postback that occurs, not a callback which is just supposed to update...
1
by: Andrew Nav | last post by:
I dragged a table from a datasource on to my web form and the GridView cpntrol was created. I turned on paging, and Edit and Delete buttons. I ran the app and everything worked great - not a...
0
by: Mike P | last post by:
I am trying to edit a gridview while using paging, but whenever I try to edit a row on a page other than page 1, I get an error. Here is my gridview and my code : <asp:GridView ID="GridView1"...
8
by: AG | last post by:
ASP.NET 2.0, VS 2005 I have a gridview with paging enabled, bound to an objectdatasource. Both were dropped on to the page and configured via the wizards. Basically working as expected. The...
8
by: Greg Lyles | last post by:
Hi all, I'm trying to develop an ASP.NET 2.0 website and am running into some real problems with what I thought would be a relatively simple thing to do. In a nutshell, I'm stuck on trying to...
1
by: DMC | last post by:
Hi, I have enabled paging in my gridview control and am now trying to work out how to programmatically access it. With my gridview i have an "Add" button which adds a new row to the gridview and...
4
by: Don Miller | last post by:
This is a repost of a reproducible problem/bug with GridView with dynamic SQL and binding. Is there a better ASP.NET newsgroup I should post to where MS techs or MVPs take an interest in such...
1
by: Evan M. | last post by:
Here's my GridView and my SqlDataSource <asp:GridView ID="ContactHistoryGrid" runat="server" AutoGenerateColumns="False" DataSourceID="ContactHistoryDS" DataKeyNames="JobHistoryID"...
6
by: McGeeky | last post by:
Hi. GridView's inbuilt capability to edit/update individual rows, one at a time, is useful but inadequate for a scenario we currently face. I want the user to be able to change values in multiple...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.