Hiding GridView column sometimes and NOT binding data for that field

Posted on 2012-08-22
Medium Priority
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
  • 3
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 38322425
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

ID: 38322567
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

dwoolley3 earned 0 total points
ID: 38347204
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

ID: 38360359
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.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

864 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