Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
</asp:GridView>


C#
GridView1.AutoGenerateColumns = true;
GridView1.DataSource = db.Snippets.ToList();
GridView1.DataBind();
//shows us that we have no columns
giveJavascriptMessage(GridView1.Columns.Count.ToString());

//I want to be able to hide specific columns at the moment this returns an index out              of range
GridView1.Columns[0].visible=false;


Im using an entity framework model to populate the gridview.

When I run this code it creates the grid-view and populates it with the table but when I try to access the columns in the grid view there are none "giveJavascriptMessage(GridView1.Columns.Count.ToString());"
returns 0

Why are no columns being created although all data is shown on the page?
How can I get the columns to be created?
Posted
Updated 28-Oct-13 0:43am
v2

1 solution

 
Share this answer
 
Comments
BenJBaker╣ 28-Oct-13 7:13am    
Thanks,

Solved it by defining the columns and disabling autogeneratecolumns

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<columns>
<asp:BoundField DataField="ID" />
<asp:BoundField DataField="Added" />
<asp:BoundField DataField="SnippetTitle" />
<asp:BoundField DataField="SnippetContent" />




Cheers
Harshil_Raval 28-Oct-13 7:19am    
Yes, that is better way.. :)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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