[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Hide Column in Gridview if data is  0

Posted on 2015-01-28
Medium Priority
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:Ed
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 29

Accepted Solution

sammySeltzer earned 2000 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.
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks


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 29

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

649 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