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

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
  • 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.
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.


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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
C# MVC Insert Multiple Row into DB 2 30
jquery + C#.net radiobuttonlist 3 30
Visual studio 2015 1 21
Access Schema 6 25
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…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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