Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys, code below suppose to hide a column but its giving me this error:
MSIL
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Source Error:


Line 28:         if (grdAvailableStuff.Columns.Count > 0)
Line 29:         {
Line 30:             grdAvailableStuff.Columns[2].Visible = false;
Line 31:
Line 32:


Check the code below, I dont knw what hapend

C#
if (grdAvailableStuff.Columns.Count > 0)
       {
           grdAvailableStuff.Columns[2].Visible = false;


       }
       else
       {
           grdAvailableStuff.HeaderRow.Cells[2].Visible = false;


           foreach (GridViewRow grd in grdAvailableStuff.Rows)
           {
               grd.Cells[2].Visible = false;


           }
       }
Posted
Comments
walterhevedeich 16-Jun-11 4:02am    
If this is databound, its possible that the binding was lost during postback. On what event did you put this code?
Anele Ngqandu 16-Jun-11 5:21am    
no data binding in the code sir
Prasad CM 16-Jun-11 5:11am    
Instead of 2 try to mention column name...
Anele Ngqandu 16-Jun-11 5:22am    
i did but its complaining that an interger is suppose to be inside here "[]" not the strings

here

grdAvailableStuff.Columns[2].Visible = false;

Means your are hiding 3rd column of grid view.

Make sure that grid view renders with 3 or more than 3 columns.
 
Share this answer
 
Comments
Anele Ngqandu 16-Jun-11 5:26am    
i know sir, but i have been changing the column number but still its giving same problems
Try this

C#
protected void grdAvailableStuff_RowCreated(object sender, GridViewRowEventArgs e)
       {
           if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
           {
               e.Row.Cells[1].Visible = false;
              
           }
       }
 
Share this answer
 

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