Reputation: 38585
Well i have a list of objects List<UserDC>
now i would want to display this in some kind of grid so i tryed the GridView
GridView1.DataSource = list
GridView1.DataBind()
Well that did work but now i want to hide some columns but that does not seem to be so easy :
GridView1.Columns(0).Visible = False
GridView1.Columns(1).Visible = False
GridView1.Columns(2).Visible = False
this just gives me a exception ArgumentOutOfRangeException
how do i make it generate the columns before it displays the list so i can filter out those that i dont want?
Upvotes: 2
Views: 7908
Reputation: 13099
To hide the GridView columns, you can use the GridView_RowDataBoundEvent and hide the unwanted columns.
You can hide the Column Header, Data Rows OR BOTH by checking the "RowType" property in this event.
Upvotes: 3
Reputation: 3094
Works OK for me. Here is a suggestion for you to check. Since this is VB .NET code, I assume you have VS 2005 or 2008. Set a breakpoint at GridView1.Columns(0).Visible = False
Using the Watch Window, create a watch for GridView1.Columns. Expand "base", What is the value of "Count"? It is likely to be ZERO, means you do not have any columns. The may be the reason you have "ArgumentOutOfRangeException".
Is your data binding to "list" working?
Upvotes: 1
Reputation: 48108
You can set your visible columns as :
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="Property 2" DataField="Property1" />
<asp:BoundField HeaderText="Property 2" DataField="Property2" />
</Columns>
</asp:GridView>
AutoGenerateColumns="false" disables exposing all properties/columns of the datasource. And BoundFields will map your properties to grids columns.
Upvotes: -1