15

This is how I navigate to myPage.aspx ,

<a href='~/myPage.aspx?show=<%#Eval("id")%>' id="showEach" runat="server">Show Each</a>

<a href="~/myPage.aspx?show=all" id="showAll" runat="server">Show All</a>

And I have a gridview in myPage.aspx

<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:BoundField  HeaderText="ColumnOne"  Visible="true"/>
<asp:BoundField  HeaderText="ColumnTwo"  Visible="true"/>
</Columns>
</asp:GridView>

What I want to do is , if Query String is equal to all(~/myPage.aspx?show=all) , I want to set GridView1's Column2's visible to true , else , set visible to false .
How can I do it ?

3
  • how and where you bind data in gridview? Commented Jul 5, 2013 at 4:49
  • it's not depends on datasource of my gridview AmitSingh , it's depends on QueryString ! Commented Jul 5, 2013 at 4:54
  • Does this answer your question? GridView Hide Column by code Commented Dec 13, 2020 at 23:20

3 Answers 3

11

You can use gridview pre-render method to set this...

protected void GridView_PreRender(object sender, EventArgs e)
    {
        if(Reqest.QueryString["Id"]=="all"&& Reqest.QueryString["Id"]!=null)
         {
           GridViewId.Columns[1].Visible = true;
         }
        else
            GridViewId.Columns[1].Visible = false;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Is there any way likes > <asp:BoundField HeaderText="ColumnTwo" Visible='if(Request.QueryString[show])==all){....}else{....}'/>
you should be using '==' rather than '='
8

you can use gridview column index to hide the particular column

Code could be

 if(Request.QueryString.Get("show")=="all")
    GridView1.Columns[1].Visible=true;
 else
    GridView1.Columns[1].Visible=false;

More detail

GridView Hide Column by code

Edit 3

Settings in ASPX/ASCX can not be done directly.

<%= %> outputs directly to the response stream, and the asp markup is not part of the response stream. Its a mistake to assume the <%= %> operators are performing any kind of preprocessing on the asp markup.

More explanation

Why will <%= %> expressions as property values on a server-controls lead to a compile errors?

Edit 1

I think yes

 <asp:BoundField HeaderText="ColumnTwo" 
      Visible='<% if (Request.QueryString.Get("all") == "all" ) "true" else "false" %>'/>

You will have to check for the syntex

Edit 2

Try this

 Visible='<% Request.QueryString.Get("all") == "all"? "true": "false"%>'

2 Comments

Is there any way likes > <asp:BoundField HeaderText="ColumnTwo" Visible='if(Request.QueryString[show])==all{....}else{....}'/>
Yes Shekhar , it's said This expression is not a valid statement :)
1

Dear try to use RowDataBound event of Grid View like

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //here apply your condition
        if(Request.QueryString["name"] == "all")
        e.Row.Cells[<index_of_cell>].Visible = true;
        else
        e.Row.Cells[<index_of_cell>].Visible = false;
    }
}

Try something like that.

Hope it works for you.

2 Comments

Is there any way likes > <asp:BoundField HeaderText="ColumnTwo" Visible='if(Request.QueryString[show])==all{....}else{....}'/>
the way you want to bind is possible but not in bound field, have to use ItemTemplate check this link stackoverflow.com/questions/1839163/… .have to do something like that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.