Thursday, July 12, 2007

How to hide GridView Column from the user

How to hide a column from user (UI) in a GridView (ASP.NET)

Solutions:

1) Set the 'visible' property for the column to 'False' in the GridView. -- This is OK if you dont want to use the column in code because this option does not render the Column at all on the UI. So, you cannot access it.

2) Use the CSS style to hide the column on UI. This option renders the column on UI but hides it. So, if you want to access it in code, you can do it.

How to apply (method 2):

a) make a css class with style 'display:none'

. HiddenCol
{
display:none;
}

b) apply this css to your column in the grid view (.aspx file)

(NOTE: Cannot include '<' '>' for tags, because the blog doesnt render them. Please use the correct tags when needed. )


columns
asp:BoundField DataField="ProductParameterUid" HeaderText="uid" ReadOnly="True"
ItemStyle CssClass="HiddenCol" /
HeaderStyle CssClass="HiddenCol" /
FooterStyle CssClass="HiddenCol" /
/asp:BoundField

/columns

NOTE: HeaderStyle and FooterStyle should be assigned same style if you dont want to display the header and footer also for that column.

Thats all and your user cannot see the column. But you can still access it. You can do this hiding in the code also. Check the following link on how to do it. http://www.netomatix.com/development/GridViewHideColumn.aspx

Hope you save some time with this.

--Shiva