本文介绍了如何在 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 事件.

我也尝试以编程方式隐藏它,但似乎无法按名称获取列,因为 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 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 08:18