Click here to Skip to main content
15,905,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have populated the grid view with BoundField property and after that I can hide a column with CSS class like following code:
C#
<asp:BoundField DataField ="FieldNamde" HeaderText="FieldName" HeaderStyle-CssClass="hidden" ItemStyle-CssClass="hidden">


The CSS Class hidden is set as following
CSS
.hidden
{
display:none;
}

Also to populate the same grid view I want to hide another column according to session state for example:
C#
if ((Session["SessionName"] != null ? (string)Session["SessionName"] : "") == "SomeText")


To be more precisely, I want to show or hide a column according to the Session and if SessionName is SomeText to show a column, if SessionName is OtherText I want to hide a column.
Thank you in advance for your reply.
Cheers.
Posted

1 solution

I found a solution and in case that someone else has the same problem just go to grid view and select the event RowCreated and after that write the code as following:
C#
protected void gvGridView_RowCreated(object sender, GridViewRowEventArgs e)
       {
             /// Session name
           if ((Session["SessionName"] != null ? (string)Session["SessionName"] : "") == "SomeText")
           {
                  /// Column that has DataField
                   if (e.Row.RowType == DataControlRowType.DataRow)
                   {
                       e.Row.Cells[0].CssClass = "hidden";
                   }
                   /// Column header
                   else if (e.Row.RowType == DataControlRowType.Header)
                   {
                       e.Row.Cells[0].CssClass = "hidden";
                   }
           }
       }


Thank you all.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900