Click here to Skip to main content
15,886,004 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Guys

Hope that everyone is well.

I am using a SelectedIndexChanged event for a button on a gridview.

The button calls the following method
public void DataView()
        {
         
           GridViewRow gvrow = UssdGridView.SelectedRow;
 
            DataKey dRow = UssdGridView.DataKeys[gvrow.DataItemIndex];
            string testID = dRow[0].ToString(); 
            var test = testID;
            string[] transEntryRecArray = test.Split('|');
            //string[] transStatus = transEntryRecArray[3].Split('=');
            string[] transDesc = transEntryRecArray[5].Split('=');
            string[] errorCode = transEntryRecArray[4].Split('=');
       
            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + " Error Code = " + errorCode[1] + "." + "\\n" + " Transaction Description = " + transDesc[1] + "." + "');", true);
        }


now when I use a PageIndexChanging event, the code
DataKey dRow = UssdGridView.DataKeys[gvrow.DataItemIndex]; 
returns a "Index was out of range. Must be non-negative and less than the size of the collection." error.

What am I doing wrong?
Posted
Comments
Er Daljeet Singh 23-Apr-14 6:21am    
Just use debugger and check whether "GridViewRow gvrow = UssdGridView.SelectedRow;" this line having value of not.
ShivaniN016 23-Apr-14 6:26am    
HI Daljeet, I can see a count of 10 which is correct.
Er Daljeet Singh 23-Apr-14 6:34am    
Check the value for "gvrow.DataItemIndex"
ShivaniN016 23-Apr-14 6:37am    
the value is 5 on the PageIndexChanging event,on the page load data which works,that value is 0
Er Daljeet Singh 23-Apr-14 6:43am    
try to maintain the value of DataItemIndex in a variable and use that variable outside the PageIndexChangedEvent

use following code in pageindexchanging event of gridview
C#
grdView.PageIndex = e.NewPageIndex;
  grdView.DataBind();
 
Share this answer
 
Comments
ShivaniN016 23-Apr-14 7:47am    
Hi Dynashwar.

I have that in place already.
[no name] 23-Apr-14 7:57am    
Use
DataKey dRow = UssdGridView.DataKeys[gvrow.SelectedIndex];
Herman<T>.Instance 23-Apr-14 8:06am    
and what if there is no SelectedIndex?
BELGIUMsky 23-Apr-14 8:11am    
sorry but your code you show right now is just the same that the user uses.
ShivaniN016 23-Apr-14 8:34am    
I do

GridViewRow gvrow = UssdGridView.SelectedRow;
I have fixed it

instead of DataKey dRow = UssdGridView.DataKeys[gvrow.selectedIndex];

I used DataKey dRow = UssdGridView.DataKeys[gvrow.RowIndex];

Thank you Dnyaneshwar
 
Share this answer
 
v3
Comments
[no name] 23-Apr-14 8:34am    
also put your gridview code
ShivaniN016 23-Apr-14 8:45am    
all sorted now ..thank you
[no name] 23-Apr-14 8:59am    
welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900