Search code examples
c#devexpressaspxgridview

Dynamic FieldName for GridViewDataTextColumn (<%# Eval(...)%>)


I have an in-memory DataSet bound to an ASPxGridView where the column names are determined by a variable. Now I would prefer to assign the FieldName of the columns in the markup with the value of the corresponding variable, but <%#Eval(...)%> throws the following error.

Parser Error Message: Databinding expressions are only supported on objects that have a DataBinding event. DevExpress.Web.ASPxGridView.GridViewDataTextColumn does not have a DataBinding event.

Is it possible to assign the FieldName-Attribute of a GridViewDataTextColumn with a variable in the markup?

From

<dx:GridViewDataTextColumn Caption="Product" FieldName="product_name">

To

<dx:GridViewDataTextColumn Caption="Product" FieldName="<%# Eval("ProductFieldName") %>">

Solution

  • No, in general. The GridViewDataTextColumn is a hierarchycal (non Data-Bound) element and it is not contained into a Data-Bound container.

    According to the exception's message, I believe this is a common situation for such an ASP.NET controls:

    DataBinding expressions are only supported on objects that have a DataBinding event.

    I believe it is possible to specify the required properties and assign column FieldName at runtime as it is illustrated here.