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.

Share:
14,288
user2026041
Author by

user2026041

Updated on June 09, 2022

Comments

  • user2026041
    user2026041 almost 2 years

    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.

  • user2026041
    user2026041 almost 9 years
    Will that hide all the columns? I only want to hide the second one (RevToDate)
  • Rahul Nikate
    Rahul Nikate almost 9 years
    @user2026041 you need to mention number in Columns[2] to hide specific column.
  • Rahul Nikate
    Rahul Nikate almost 9 years
    I'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 in Columns[i] 3] OP wants to hide column based on user role. Hope it make sense.
  • waleedansari
    waleedansari almost 9 years
    Means u are trying to say, I should spoon feed the answer. ok
  • Rahul Nikate
    Rahul Nikate almost 9 years
    Please have look in "How to answer on SO"
  • waleedansari
    waleedansari almost 9 years
    @RahulNikate better now?
  • waleedansari
    waleedansari almost 9 years
    Nah. 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
    Rahul Nikate almost 9 years
    Read 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
    Rahul Nikate almost 9 years
    Sorry 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
    waleedansari almost 9 years
    yes agreed totally. thanks for the right direction :)