Solved

Hide Column in Gridview if data is  0

Posted on 2015-01-28
6
499 Views
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">
            <Columns>
                <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">
                    <EditItemTemplate>

                                <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>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <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:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <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" />
            </Columns>
        </asp:GridView>

Open in new window

0
Comment
Question by:edjones1
  • 3
  • 2
6 Comments
 
LVL 22

Expert Comment

by:Mrunal
ID: 40577010
Hi,
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.
0
 
LVL 28

Accepted Solution

by:
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">
            <ItemTemplate>
                <asp:Label Text='<%# Eval("NoPreviousQualEvidence") %>' ID="lblNoPrev" 
                            Visible='<%# !String.IsNullOrEmpty(Convert.ToString(Eval("NoPreviousQualEvidence"))) %>'
                            runat="server" />
            </ItemTemplate>
        </asp:TemplateField>

Open in new window

0
 

Expert Comment

by:whiwex
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.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Expert Comment

by:whiwex
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.
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 40836065
I don't understand the issue.

Could you please be a bit clearer?
0
 

Expert Comment

by:whiwex
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.



Thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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