Click here to Skip to main content
15,897,315 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I bind a dataset to gridview and after that when i want to unvisible the columns, it gets the following error:
SQL
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


what should i do?
this is my code:
C#
GridView1.DataSource = myDataSet;
        GridView1.DataKeyNames = new string[] { "IsRead" };
        GridView1.DataBind();
        GridView1.Columns[0].Visible = false;
        GridView1.Columns[5].Visible = false;
        GridView1.Columns[6].Visible = false;
        GridView1.Columns[7].Visible = false;
        GridView1.Columns[8].Visible = false;
Posted
Updated 5-Mar-13 23:13pm
v2
Comments
Raje_ 6-Mar-13 4:49am    
how are you hiding the column,can you post the codes?
J.Karthick 6-Mar-13 4:52am    
This particular error you mention is because of your datasource which you bind with gridview is 'NULL'. Check the condition( != null) before assign datasource to grid.
VishwaKL 6-Mar-13 4:53am    
which event you using to bind/hide the data?

If you want to invisible your column in gridview,
specify <HeaderTemplate> property 'visible = "False"'

or

Remove particular column before you assign datasource to gridview.
 
Share this answer
 
v2
Comments
MJ MOUSAVI 6-Mar-13 5:12am    
I need those columns and i put them in datakeynames
but i do not want to show these columns in web page
so i can not remove them and also i can not put header template visible property to false because i bind data to grid dynamically.
in this site i found the solution
http://forums.asp.net/t/1086562.aspx/1[^]
 
Share this answer
 
Comments
giri001 6-Mar-13 5:53am    
+4 because its working solution.

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