Solved

Hide Column in Gridview if data is  0

Posted on 2015-01-28
6
485 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Name space syntax error 12 43
consuming an asmx web service in winforms application 3 24
C# Single Form 8 28
execute SQL Stored Procedure 1000 times from vb.net 3 25
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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 …

896 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now