User M
User M

Reputation: 329

Hide set of columns in a Grid View whose datasource is a custom class

I have custom class which has 10 properties in it. I have to display only five of those as columns in a grid view. So far I had tried this:

gridView1.DataSource = reservation; // This is a List of ReservationDomain (Custom Class with the properties I want to populate in the gridview)
gridView1.DataBind();
gridView1.Columns[2].Visible = false; // At this point of time the Columns count is ZERO, so an exception is thrown.

What's the way to do it. I searched but every where I could find the same way of doing it or through LINQ, which internally is also doing the same thing.

Upvotes: 0

Views: 859

Answers (2)

Nitin Chaurasia
Nitin Chaurasia

Reputation: 263

<asp:GridView ID="gvUserInfo" runat="server" >
<Columns>
<asp:BoundField DataField="Column1" HeaderText="Column1" Visible="False" />
<asp:BoundField DataField="Column2" HeaderText="Column2" />
</Columns>
</asp:GridView>

Upvotes: 1

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73492

You need Browsable(false) attribute

public class MyClass
{
    [Browsable(false)]
    public int MyProperty {get;set;}//property you don't want to show
}

You need to set this in your custom class's property I think in your case you need to set this in ReservationDomain class

Edit

    GridView1.RowCreated -= GridView1_RowCreated;
    GridView1.RowCreated += GridView1_RowCreated;

    void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[1].Visible = false;
    }

Hope this helps

Upvotes: 1

Related Questions