C# 有条件地显示隐藏 asp.net Gridview 列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17481250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:35:52  来源:igfitidea点击:

conditionally show hide asp.net Gridview column

c#asp.netgridviewvisible

提问by zey

This is how I navigate to myPage.aspx,

这就是我导航到的方式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

我有一个网格视图 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 ?

我想要做的是,如果查询字符串等于all(~/myPage.aspx?show=all) ,我想将 GridView1 的Column2可见设置为 true ,否则,将可见设置为 false 。
我该怎么做 ?

采纳答案by ????

you can use gridview column index to hide the particular column

您可以使用 gridview 列索引来隐藏特定列

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

GridView 按代码隐藏列

Edit 1

编辑 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

编辑 2

Try this

尝试这个

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

Edit 3

编辑 3

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.

<%= %> 直接输出到响应流,asp 标记不是响应流的一部分。假设 <%= %> 运算符对 asp 标记执行任何类型的预处理是错误的。

More explanation

更多解释

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

为什么 <%= %> 表达式作为服务器控件上的属性值会导致编译错误?

回答by Amit Singh

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

您可以使用 gridview 预渲染方法来设置此...

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;
    }

回答by Rahul

Dear try to use RowDataBoundevent of Grid View like

亲爱的尝试使用Grid View 的RowDataBound事件,例如

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.

希望对你有效。