只有使用C#asp.net具有DataBinding事件的对象才支持数据绑定表达式

时间:2018-03-22 11:06:22

标签: c# asp.net webforms

我收到以下错误。

  

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

我在网格视图BoundField中执行可见条件。以下是我的代码

<asp:BoundField DataField="AssessmentStartdate" 
    HeaderText="Assessment Date" 
    DataFormatString="{0:d}" 
    SortExpression="AssessmentStartdate" 
    HeaderStyle-BackColor="#f1f1f1" 
    Visible='<%# Eval("DayType").ToString()=="Single" %>' >
</asp:BoundField>

相同的可见条件正在使用TemplateField。为什么可见条件不适用于boundfield。请用代码等待你的回答。

1 个答案:

答案 0 :(得分:0)

您可以在执行DataBind之前在运行时隐藏该列。在下面的代码中,我假设dayTypeDataTable的一部分,并且在整个表中它是相同的,正如你所说的想要隐藏整个列我假设值是同样的。

var data = GetData();
string dayType = string.Empty;
int boundFieldIndex = 4;

if(data.Tables[0].Rows.Count > 0)
{
    dayType = data.Tables[0].Rows[0].Field<string>("dayType");
}

gridView.Columns[boundFieldIndex].Visible = dayType == "Single";
gridView.DataSource = data.Tables[0];
gridView.DataBind();