Pergunta

I use an ASPxGridView bound to an XPODataSource with AutoGenerateColumns=true. The datasource's table/class name is set through code, so i cannot prepare grid columns in markup (as different tables have different column names).

I would like to use DataItemTemplate to customize a specific column of each table, but in all examples it is used inside a column created in markup (example: http://demos.devexpress.com/aspxgridviewdemos/Templates/Template.aspx).

So how can i create and assign a DataItemTemplate to a dynamically created column?

Foi útil?

Solução

Answer is here, should have googled more: http://www.devexpress.com/Support/Center/Example/Details/E293

Basically, you create the template as a class:

class MyHyperlinkTemplate : ITemplate {
    public void InstantiateIn(Control container) {
        ASPxHyperLink link = new ASPxHyperLink();
        GridViewDataItemTemplateContainer gridContainer = (GridViewDataItemTemplateContainer)container;
        link.NavigateUrl = string.Format("~/details.aspx?Device={0}", gridContainer.KeyValue);
        link.Text = string.Format("Get details about device {0}", gridContainer.KeyValue);
        container.Controls.Add(link);
    }
}

then assign it to a column:

((GridViewDataColumn)ASPxGridView1.Columns["colItemTemplate"]).DataItemTemplate = new MyHyperlinkTemplate();
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top