' id="showEach" r...">

Conditionally show hidden asp.net Gridview column

This is how I go 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 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 if Query String is all (~ / myPage.aspx? Show = all), I want GridView1 Column2 display as true, else, set visible to false.
How can i do this?

+8
c # gridview visible
source share
3 answers

you can use gridview column index to hide a specific column

Code may be

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

More details

GridView Hide column by code

Change 1

I think yes

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

You will need to check syntex

Edit 2

try it

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

Edit 3

Impossible to execute directly.

<% =%> outputs directly to the response stream, and asp markup is not part of the response stream. It is a mistake to assume that the <% =%> operators do any preprocessing on asp markup.

Additional Information

Why will there be <% =%> expressions as property values ​​on the management server lead to compilation errors?

+8
source share

You can use the gridview pre-rendering 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; } 
+7
source share

An expensive example of using a RowDataBound is a Grid View event similar to

 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 this.

Hope this works for you.

+1
source share

All Articles