Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide a column in a DataGrid?

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.

like image 987
user2026041 Avatar asked Jun 04 '15 09:06

user2026041


People also ask

How do I hide a column in react grid?

Example of Show or Hide Column in React Grid ComponentClick column name from the toolbar to toggle visibility.

How do I hide a column in Mui table?

By opening the column menu and clicking the Hide menu item. By clicking the Columns menu and toggling the columns to show or hide.

What is DataGridView in C#?

The DataGridView control provides a customizable table for displaying data. The DataGridView class allows customization of cells, rows, columns, and borders through the use of properties such as DefaultCellStyle, ColumnHeadersDefaultCellStyle, CellBorderStyle, and GridColor.


1 Answers

You can do like this.

 if (!CurrentUser.IsInRole("Admin"))
 {
     this.dgCustomers.Columns[2].Visible = false;
     btnDelete.Visible = false;
     btnUpload2.Visible = false;
 }
like image 60
Rahul Nikate Avatar answered Sep 29 '22 06:09

Rahul Nikate