Hide Column in Gridview if data is  0

Posted on 2015-01-28
Last Modified: 2015-06-17
Whats the best way to hide a gridview column if the data in a particular column is null or 0  ?

Below is the code to the gridview...

What I want to do is hide the TemplateFields  if the bound item 'NoPreviousQualEvidence' is 0 or Null

<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" DataKeyNames="TraineeID" DataSourceID="SqlDataSource4">
                <asp:CommandField ShowEditButton="True" />
                <asp:BoundField DataField="TraineeID" HeaderText="TraineeID" ReadOnly="True" SortExpression="TraineeID" />
                <asp:CheckBoxField DataField="UKLegalRightEvidence" HeaderText="UKLegalRightEvidence" SortExpression="UKLegalRightEvidence" />
                <asp:CheckBoxField DataField="PreviousQualEvidence" HeaderText="PreviousQualEvidence" SortExpression="PreviousQualEvidence" />
                <asp:TemplateField HeaderText="NoPreviousQualEvidence" SortExpression="NoPreviousQualEvidence">

                                <asp:DropDownList ID="ddlNoReasonEdit" runat="server"  SelectedValue='<%# Bind("NoPreviousQualEvidence") %>'>
                                                        <asp:ListItem Value="0">Select Reason</asp:ListItem>
                                                        <asp:ListItem Value="1">No - Quali</asp:ListItem>
                                                        <asp:ListItem Value="2">No - Evidence </asp:ListItem>
                        <asp:DropDownList ID="ddlNoReasonEdit" runat="server" SelectedValue='<%# Bind("NoPreviousQualEvidence") %>'>
                            <asp:ListItem Value="0">Select Reason</asp:ListItem>
                            <asp:ListItem Value="1">No - No Qualifications</asp:ListItem>
                            <asp:ListItem Value="2">No - No Evidence Provided</asp:ListItem>
                <asp:CheckBoxField DataField="EmploymentEvidence" HeaderText="EmploymentEvidence" SortExpression="EmploymentEvidence" />
                <asp:CheckBoxField DataField="AgeEvidence" HeaderText="AgeEvidence" SortExpression="AgeEvidence" />
                <asp:CheckBoxField DataField="EmployerEvidence" HeaderText="EmployerEvidence" SortExpression="EmployerEvidence" />
                <asp:CheckBoxField DataField="HomeAddressEvidence" HeaderText="HomeAddressEvidence" SortExpression="HomeAddressEvidence" />
                <asp:BoundField DataField="LOGGEDIN_ID" HeaderText="LOGGEDIN_ID" SortExpression="LOGGEDIN_ID" />

Open in new window

Question by:edjones1
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 22

Expert Comment

ID: 40577010
you can try with RowDataBound event of gridview. In this event  you can find dropdownlist and check its value and accordingly you can set visibility of column. But with this you can not hide complete column.

If there is not comparison of data then you can do it in ItemCreated event, but you need to check the value binded to cell. so better way, you can do it in Prerender event of gridview and loop through all rows and after finding that specific cell's value you can hide column.

Hope this helps you.
LVL 28

Accepted Solution

sammySeltzer earned 500 total points
ID: 40578491
Either that *OR* you use label within TemplateField to achieve your desired result.

        <asp:TemplateField HeaderText="NoPreviousQualEvidence" SortExpression="NoPreviousQualEvidence">
                <asp:Label Text='<%# Eval("NoPreviousQualEvidence") %>' ID="lblNoPrev" 
                            Visible='<%# !String.IsNullOrEmpty(Convert.ToString(Eval("NoPreviousQualEvidence"))) %>'
                            runat="server" />

Open in new window


Expert Comment

ID: 40836058
Yes I can use the templatefield the way sammySeltzer explains and it works. The problem is the number of template  fields will vary depending on what the user selects. i.e. sometimes I will omly need 2 checkboxes and sometimes I may need 5 check boxes.
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.


Expert Comment

ID: 40836061
There is a link button on the page that when clicked it will check each check box and determine if it is checked or not. This is where I can't find the control.
LVL 28

Expert Comment

ID: 40836065
I don't understand the issue.

Could you please be a bit clearer?

Expert Comment

ID: 40836103
Here's my datatable
LastName, Firstname, Homeroom
smith            mary         1-a
Jones            bill             1-b

Then depending on how many homerooms I add a column for each homeroom. So for 2 homerooms then the datatable looks like this
LastName, Firstname, Homeroom, Blankcolumn1,  Blankcolumn2
smith            mary         1-a
Jones            bill             1-b

I then add a check box to the blank columns so it looks like this:
LastName, Firstname, Homeroom, Blankcolumn1,  Blankcolumn2
smith            mary         1-a              CheckBox1-a      checkbox1-b
Jones            bill             1-b             CheckBox1-a      checkbox1-b

There is a link button that when clicked goes through each row in the gridview and checks each check box
to see if the check box is clicked.  The problem is the findcontrol doesn't find the checkbox to see if it is checked or not. That is where the error happens.


Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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