Hiding GridView column sometimes and NOT binding data for that field

Posted on 2012-08-22
Last Modified: 2012-09-03
I am able to make columns visible or invisible in the Code-Behind of my ASP.NET page. I am doing this in event RowCreated rather than RowDataBound since the performance is better.

e.Row.Cells[i].Visible = true;
e.Row.Cells[i].Visible = false;

Open in new window

One particular field (called Entry) can have MAX characters, so it is usually very large. However, there is one client whose records have a small amount of data in the Entry field and I would like to display this field for them only. Again, I am able to toggle the cell based on my criteria, but I notice that the data for the field is ALWAYS bound regardless of whether I am displaying the column or not. Thus, I am getting poor performance when displaying other customer records since the huge fields are being databound, it seems, even though it is not being displayed.
<asp:BoundField DataField="Entry" HeaderText="Entry" SortExpression="Entry"  />

Open in new window

I notice that if I make the BoundField invisible all the time, then no data is bound for the Entry field (for any customer) but, of course, then I can't display the data for the one particular customer that has small data. . . . This requires further explanation: By setting the control's Visible property to false, I am not able to toggle it to being visible in the Code-Behind in RowDataBound, for some reason. Thus, I have been removing the visible property from the ASP.NET control BoundField and set it only in the Code-Behind.

<asp:BoundField DataField="Entry" HeaderText="Entry" SortExpression="Entry" visible="false" />

Open in new window

Can anyone help me to display my desired column and perform a Data Bind only when I am displaying it?
Question by:dwoolley3
    LVL 13

    Expert Comment

    by:Jesus Rodriguez
    try to do something like this, add the Visible condition to be binding as a return of a Function

    visible="<% RetValueToSet(Eval("Entry")) %>"

    and then on the Function do this on a vbScript

    <script type="vbscript" runas="server">
    Function RetValueToSet(EntryField as string)
      If Entry="1" then 'Condition base on the field
          return True
          return False
       End If
    End Function

    Author Comment

    Great idea.

    However, when I use the boolean return value inside the visible property, Visual Studio does not compile it and gives me this error:

    Cannot creat an object of type 'System.Boolean' from its string representation '<% RetValueToSet(Eval('GroupCode')) %>'

    The code I am using is the following:

      <script type="vbscript" runat="server">
          Function RetValueToSet(GroupCode as string) as Boolean
              If GroupCode="Commercial" then   'Condition based on the field
                   return True
                   return False
              End If
         End Function
    <asp:BoundField DataField="Entry" HeaderText="Entry" 
        visible="<% RetValueToSet(Eval('GroupCode')) %>"
        />  <%--visible="false"--%>

    Open in new window

    I am open to suggestions on correcting this, since it is a neat idea that I did not know how to utilize before.

    One correction to my prior post, based on further experimentation...
    After setting the control's Visible property to false, I was able to toggle it to be visible in the Code-Behind in RowDataBound

     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            int i = 25; //Entry is 25 out of 0..48
            if (GroupCodeAtTop == "Commercial")
                GridView1.Columns[i].Visible = true; 
                GridView1.Columns[i].Visible = false;

    Open in new window

    However, once it becomes true, then it seems that the performance issue starts once I change to a different customer. It seems that the data is binding for the next customer even though I set the visible property back to false. Maybe it is not being done soon enough?

    Accepted Solution

    I was able to set the Visibility for the column when the user changed GroupCode (event ddlGroupCodeAtTop_SelectedIndexChanged), which satisfied my needs:

            int c = 23;  //Currently field Entry is in position 23 of GridView, but recheck it
            if (ddlGroupCodeAtTop.SelectedValue == "Commercial")
                GridView1.Columns[c].Visible = true;        
                GridView1.Columns[c].Visible = false; 

    Open in new window


    Author Closing Comment

    The code provided by a different user did not work. I was able to resolve the issue by changing the column's visibility at the time that the user changes the drop down option for the Group Code.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now