如何隐藏DataGrid中的列?

时间:2015-06-04 09:58:05

标签: c# css asp.net visual-studio datagrid

对于任何非管理员用户,我需要隐藏RevToDate中的DataGrid列。如何仅隐藏此列?

 <asp:DataGrid runat="server" CssClass="tblResults" OnItemDataBound="dgList_ItemCreated" AllowSorting="true" OnSortCommand="dgCustomer_Sort" ID="dgCustomers" DataKeyField="ID" AutoGenerateColumns="false">
            <HeaderStyle CssClass="tblResultsHeader" />
            <AlternatingItemStyle BackColor="#EEEEEE" />
            <Columns>
                <asp:HyperLinkColumn ItemStyle-CssClass="loading" DataNavigateUrlFormatString="Customer.aspx?CustID={0}" DataNavigateUrlField="ID" DataTextField="AccountCode" HeaderText="A/C Code" SortExpression="AccountCode"></asp:HyperLinkColumn>
                <asp:BoundColumn DataField="CurrencyDesc" HeaderText="Currency" SortExpression="CurrencyDesc"></asp:BoundColumn>
                <asp:BoundColumn DataField="RevToDate" HeaderText="Rev To Date (Net)" SortExpression="RevToDate"></asp:BoundColumn>
                <asp:BoundColumn DataField="CreditLimitAmount" HeaderText="Credit Limit" SortExpression="CreditLimitAmount"></asp:BoundColumn>
                <asp:BoundColumn DataField="DiscountReviewDate" HeaderText="Discount Review Date" SortExpression="DiscountReviewDate" Visible="false"></asp:BoundColumn>
            </Columns>
 </asp:DataGrid

我正在使用此代码隐藏某些项目:

 if (!CurrentUser.IsInRole("Admin"))
    {
        btnDelete.Visible = false;
        btnUpload2.Visible = false;
    }

但我不确定如何隐藏专栏。我无法在CSS中将Visible设置为false,因为它会隐藏所有用户的列。

2 个答案:

答案 0 :(得分:4)

你可以这样做。

 if (!CurrentUser.IsInRole("Admin"))
 {
     this.dgCustomers.Columns[2].Visible = false;
     btnDelete.Visible = false;
     btnUpload2.Visible = false;
 }

答案 1 :(得分:2)

从aspx页面可以看出它是真的,如:

<asp:BoundColumn visible="true" DataField="RevToDate" HeaderText="Rev To Date (Net)" SortExpression="RevToDate"></asp:BoundColumn>

然后从代码中使其不可见:

if (!CurrentUser.IsInRole("Admin"))
        {
         this.gdCustomers.Columns[2].Visible = false;
         btnDelete.Visible = false;
         btnUpload2.Visible = false;
        }

其中2是gridview中的列索引。