简体   繁体   中英

Databinding expressions are only supported on objects that have a DataBinding event using C# asp.net

I am getting below error.

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

I am doing visible condition in grid view BoundField. Below is my code

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

Same visible condition is working using TemplateField. Why visible condition is not working with boundfield. Please waiting for your answer with code.

You can hide the column at runtime before performing the DataBind . In the following code, I'm assuming that dayType is part of the DataTable , and it's the same across the table, As you said that you want to hide the entire column I assume that the value is the same.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM