如何在GridView中隐藏TemplateField列?

我try 了以下方法:

<asp:TemplateField ShowHeader="False" Visible='<%# MyBoolProperty %>' >
<ItemTemplate>
    <asp:LinkButton ID="attachmentButton" runat="server" ... />
</ItemTemplate>

但它不起作用,并给出以下错误:

只有具有数据绑定事件的对象才支持数据绑定表达式.

我还试图以编程方式隐藏它,但似乎不可能按名称获取列,因为TemplateField列没有名称.

推荐答案

protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
         e.Row.Cells[columnIndex].Visible = false;
}


If you don't prefer hard-coded index, the only workaround I can suggest is to provide a HeaderText for the GridViewColumn and then find the column using that HeaderText.

protected void UsersGrid_RowCreated(object sender, GridViewRowEventArgs e)
{
    ((DataControlField)UsersGrid.Columns
            .Cast<DataControlField>()
            .Where(fld => fld.HeaderText == "Email")
            .SingleOrDefault()).Visible = false;
}

Asp.net相关问答推荐

授权错误错误 400:C# 上的 redirect_uri_mismatch

如何防止 IISExpress 和我的网站文件夹弄乱我的文档文件夹?

web.config 部分的单独配置文件

在视图 Mvc.net 中访问路由值

RestSharp 不反序列化 JSON 对象列表,始终为 Null

Twitter bootstrap glyphicons 不会出现在发布模式 404 中

通过 jQuery 调用 ASP.NET 服务器端方法

如何在 C# 中转换 TryCast?

ASP.NET 中的全局资源与本地资源

svg 无法在 localhost 上的 IIS 网络服务器上运行

WebForm_PostBackOptions 文档

ASP.NET 5、EF 7 和 SQLite - SQLite 错误 1:没有这样的表:博客

使 WCF Web 服务与 GET 请求一起工作

将 C# ASP.NET 数组传递给 Javascript 数组

在 ASP.NET MVC 中模拟 User.Identity

如何判断字符串的第一个字符是否是字母,C#中的任何字母

x-powered by是什么意思?

将字符串转换为时间

在 Visual Studio 2012 中调试 .NET Framework 源代码?

如何使用 WebAPI 处理图像