GridView:订单列,显示/隐藏列

时间:2013-02-06 16:46:04

标签: asp.net vb.net-2010

好的......所以我正在尝试使用GridView从我的数据库中的表中显示数据。我需要更改列的显示顺序,我需要显示/隐藏某些列,具体取决于用户浏览网站的语言(法语或英语)。

到目前为止,在我的代码中,我有这个。数据通过DataSource加载。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    LoadS25ATransactionsData()

End Sub


Private Sub LoadS25ATransactionsData()

    gvS25ATransactions.Visible = True
    gvS25ATransactions.DataSource = ViewS25A.GetS25ATransactions
    gvS25ATransactions.DataBind()

End Sub

我已经尝试了很多东西来改变列的顺序并隐藏和显示其他但无济于事。请帮忙!

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用GridView.Columns [0]按索引隐藏列或隐藏名称,如下例所示。

<asp:GridView runat="server" ID="GridView1" 
   AutoGenerateColumns="False" OnPreRender="GridView_PreRender">
    <Columns>
        <asp:BoundField DataField="LastName" HeaderText="LastName" />
        <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
    </Columns>
</asp:GridView>

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        var users = new List<User>
            {
                new User {FirstName = "John", LastName = "Doe"},
                new User {FirstName = "Marry", LastName = "Newton"},
                new User {FirstName = "Joe", LastName = "Black"}
            };

        GridView1.DataSource = users;
        GridView1.DataBind();
    }
}

protected void GridView_PreRender(object sender, EventArgs e)
{
    foreach (DataControlField column in GridView1.Columns)
        if (column.HeaderText == "FirstName")
            column.Visible = false;
}