2

I have written below lines of code in order to hide and show column in gridview based on the condition.

 <asp:TemplateField HeaderText="FirstName" Visible='<%# Eval("FirstName") != null ? true:false %>'>
    <ItemTemplate>
       <asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
    </ItemTemplate>
 </asp:TemplateField>

It's not working!

ozil
  • 6,930
  • 9
  • 33
  • 56
  • Are you sure that `FirstName` is `null` and not an empty string? – Ben Aug 06 '15 at 06:26
  • Refer this http://stackoverflow.com/a/17481363/795683 – Sain Pradeep Aug 06 '15 at 06:26
  • Most likely FirstName will be String.Empty and not null. Also change true:false to "true" : "false" – Martin Aug 06 '15 at 06:31
  • 1
    Note that a template field will be evaluated for each row. You may have rows where First name is populated and rows when it is not. What is your expectation then? Look at other GridView events to handle this like `bound` and `rowDataBound`. You could have a variable that tracks if FirstName is ever populated. Update this in the `rowDataBound` event. Then in the `bound` event, check the variable and set the column visibility accordingly. – Jon P Aug 06 '15 at 06:59

2 Answers2

1

Update in code as below :

<asp:TemplateField HeaderText="FirstName" Visible='<%= !string.IsNullOrEmpty(Eval("FirstName")) ? "true" : "false" %>'>
    <ItemTemplate>
         <asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

Edit

<asp:TemplateField HeaderText="FirstName" Visible='<%# !string.IsNullOrEmpty(Eval("FirstName")) ? "true" : "false" %>'>
    <ItemTemplate>
         <asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>
Krunal Mevada
  • 1,637
  • 1
  • 17
  • 28
  • It throws Cannot create an object of type 'System.Boolean' from its string representation '<%= !string.IsNullOrEmpty(Eval("FirstName")) ? "true" : "false" %>' for the 'Visible' property. –  Aug 06 '15 at 06:46
  • 1
    `'<%# !string.IsNullOrEmpty(Eval("FirstName")) ? "true" : "false" %>'` – Krunal Mevada Aug 06 '15 at 12:26
0

Grid View

<asp:GridView ID="GridDisplay" runat="server" AutoGenerateColumns="False" BorderStyle="Groove"
                BorderWidth="1px" BorderColor="ActiveCaptionText" Width="100%" EmptyDataText="No Record Found"
                AllowPaging="True" OnPageIndexChanging="GridDisplay_PageIndexChanging" AllowSorting="True"
                OnRowEditing="GridDisplay_RowEditing" OnRowDeleting="GridDisplay_RowDeleting"
                CssClass="listbooking ListingTable">
                <Columns>
                    <asp:TemplateField HeaderText="Booking Code">
                        <ItemTemplate>
                            <%# Eval("id")%>
                        </ItemTemplate>
                        <HeaderStyle Width="5%" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Property Code">
                        <ItemTemplate>
                            <%# Eval("property_id")%>
                        </ItemTemplate>
                        <HeaderStyle Width="5%" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Name">
                        <ItemTemplate>
                            <%# Eval("sales_username")%>
                        </ItemTemplate>
                        <HeaderStyle Width="18%" />
                    </asp:TemplateField>                                         
                </Columns>              
            </asp:GridView>

i am trying to hide 2 columns(name).

During Binding the grid data

this.GridDisplay.Columns[2].Visible = false;
Prasad Raja
  • 685
  • 1
  • 9
  • 37