tags:

views:

701

answers:

4

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?

A: 

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.

Canavar
A: 

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?

StartClass0830
+1  A: 

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.

Sachin Gaur
To use the GridView_RowDataBoundEvent is a overkill. Not sure you can really do that. Using the property Visible is much better.
StartClass0830
A: 

You can't do this when auto-generating columns.

Dean Madden