How to hide a column in a DataGrid?
14,288
Solution 1
You can do like this.
if (!CurrentUser.IsInRole("Admin"))
{
this.dgCustomers.Columns[2].Visible = false;
btnDelete.Visible = false;
btnUpload2.Visible = false;
}
Solution 2
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.
Author by
user2026041
Updated on June 09, 2022Comments
-
user2026041 almost 2 years
I need to hide the
RevToDate
column in theDataGrid
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.
-
user2026041 almost 9 yearsWill that hide all the columns? I only want to hide the second one (RevToDate)
-
Rahul Nikate almost 9 years@user2026041 you need to mention number in
Columns[2]
to hide specific column. -
Rahul Nikate almost 9 yearsI'm not the downvoter but still wanted to explain reason. 1] It seems you have copied and pasted code here because it doesn't match with OP question. 2] What is
i
inColumns[i]
3] OP wants to hide column based on user role. Hope it make sense. -
waleedansari almost 9 yearsMeans u are trying to say, I should spoon feed the answer. ok
-
Rahul Nikate almost 9 yearsPlease have look in "How to answer on SO"
-
waleedansari almost 9 years@RahulNikate better now?
-
waleedansari almost 9 yearsNah. I have made it more clear or i would say crystal clear by writing the code for aspx as well. Total spoon feed.
-
Rahul Nikate almost 9 yearsRead this again "I need to hide the RevToDate column in the DataGrid for any user who is not admin" and you are doing perfectly reverse to it. Bad spoon feed.
-
Rahul Nikate almost 9 yearsSorry don't feel offended. Just tried to guide you on SO. This answers will be very helpful to future user's. So we have to be very clear about what we are writing here.
-
waleedansari almost 9 yearsyes agreed totally. thanks for the right direction :)