Reputation: 131
I need to hide the RevToDate
column in the DataGrid
for any user who is not admin. How do I hide only this column?
<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
I'm using this code to hide certain items:
if (!CurrentUser.IsInRole("Admin"))
{
btnDelete.Visible = false;
btnUpload2.Visible = false;
}
But I am not sure how to hide the column. I can't set Visible to false in the CSS because it will hide the column from all users.
Upvotes: 5
Views: 17008
Reputation: 197
Make it visible true from the aspx page like:
<asp:BoundColumn visible="true" DataField="RevToDate" HeaderText="Rev To Date (Net)" SortExpression="RevToDate"></asp:BoundColumn>
and then from code make it invisible:
if (!CurrentUser.IsInRole("Admin"))
{
this.gdCustomers.Columns[2].Visible = false;
btnDelete.Visible = false;
btnUpload2.Visible = false;
}
Where 2 is the column index in your gridview.
Upvotes: 2
Reputation: 6337
You can do like this.
if (!CurrentUser.IsInRole("Admin"))
{
this.dgCustomers.Columns[2].Visible = false;
btnDelete.Visible = false;
btnUpload2.Visible = false;
}
Upvotes: 9