Conditional Hide GridView Template Column

1.1k Views Asked by At

I'm trying to conditionally hide a template column within the Gridview, I've tried the following, but this doesn't work. Is there a way to do this within the .aspx?

 <asp:TemplateField HeaderText="Grade" SortExpression="grade" Visible='<%# Convert.ToDouble(Eval("grade")) == 0 ? true : false %>'>
           <ItemTemplate>
                <%# string.Format("{0:0.#}", Convert.ToDouble(Eval("grade"))) %>
           </ItemTemplate>
 </asp:TemplateField>
1

There are 1 best solutions below

0
On

you can do all of your conditional formatting (colors, hide etc.) by using the itemDataBound event. However, your sample markup does not seem to have a control - and you want to have a control for that information. And that control should have some kind of "id" or name that you can refernce in code.

However, since you did not provide much of the markup? Then I give a example for both the cells collection, and the controls that you might have.

So, say we have this simple grid view. Note how the last two fields are both City, one is a bound field (often common), and the other is a label (but could be a text box or just about any asp.net control).

      <asp:GridView ID="GridView1" runat="server" CssClass="table-hover" AutoGenerateColumns="False" DataKeyNames="ID">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                <asp:BoundField DataField="HotelName" HeaderText="HotelName" SortExpression="HotelName" />
                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />

                <asp:TemplateField HeaderText ="City2">
                    <ItemTemplate>
                        <asp:Label ID="lblCity" runat="server" Text = '<%# Eval("City") %>'  >'<"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Ok, on page load, we load up the above grid with this code:

protected void Page_Load(object sender, System.EventArgs e)
{
    if (IsPostBack == false)
        LoadGrid();
}

public void LoadGrid()
{
    using (SqlCommand cmdSQL = new SqlCommand("SELECT ID, FirstName, LastName, HotelName, City from tblHotels", new SqlConnection(My.Settings.TEST3)))
    {
        cmdSQL.Connection.Open();
        GridView1.DataSource = cmdSQL.ExecuteReader;
        GridView1.DataBind();
    }
}

Ok, we get this output:

enter image description here

Now, lets hide a column, based on say city. We say City = "Edmonton", we hide the control.

So, you use the itemData bound event.

I have in this sample both the bound fields (cells) collection hide example, and also for how do you do this with templated controls (find control).

So, the code is this:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
       GridViewRow gvRow = e.Row;

       // hide a Bound field
       TableCell f = gvRow.Cells(4);
       if (f.Text == "Edmonton")
           f.Style("Display") = "none";

       // hide a templated control

       Label lbl = gvRow.FindControl("lblCity");
       if (lbl.Text == "Edmonton")
           lbl.Style("Display") = "none";
   }
}

And now the output is this:

enter image description here