0

我有自定义类,其中有 10 个属性。我只需要在网格视图中将其中的五个显示为列。到目前为止,我已经尝试过这个:

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.

有什么办法。我搜索了所有可以找到相同方法或通过 LINQ 的地方,它在内部也在做同样的事情。

4

2 回答 2

1
<asp:GridView ID="gvUserInfo" runat="server" >
<Columns>
<asp:BoundField DataField="Column1" HeaderText="Column1" Visible="False" />
<asp:BoundField DataField="Column2" HeaderText="Column2" />
</Columns>
</asp:GridView>
于 2013-07-26T09:53:19.217 回答
1

你需要Browsable(false)属性

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

您需要在自定义类的属性中设置它我认为在您的情况下您需要在ReservationDomain类中设置它

编辑

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

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

希望这可以帮助

于 2013-07-24T16:33:55.817 回答