如何在 GridView 中隐藏 TemplateField 列 [英] How to hide a TemplateField column in a GridView

查看:129
本文介绍了如何在 GridView 中隐藏 TemplateField 列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 GridView 中隐藏 TemplateField 列?

How can I hide a TemplateField column in a GridView?

我尝试了以下方法:

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

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

but it didn't work and gives the following error:

数据绑定表达式仅在具有数据绑定事件的对象上受支持.System.Web.UI.WebControls.TemplateField 没有 DataBinding 事件.

Databinding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.TemplateField does not have a DataBinding event.

我也尝试以编程方式隐藏它,但似乎无法按名称获取列,因为 TemplateField 列没有名称.

I tried also to hide it programmatically, but seems it's not possible to get a column by the name because there iss no name for TemplateField column.

推荐答案

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


如果您不喜欢硬编码索引,我建议的唯一解决方法是GridViewColumn 提供一个 HeaderText,然后使用 GridViewColumn 找到该列代码>HeaderText.


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

这篇关于如何在 GridView 中隐藏 TemplateField 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆