문제

How can I create a template in c# for an AXPxGridViewDataTextColumn without any markup and with using Eval to display the DataItem value?

-The problem that I am having is that the string "<%#Eval("dataTableField1")%>" shows up in the GridView for every row instead of the appropriate values.

Here is an example of my attempt:

    public override void DataBind()
    {
        ...
        GridViewDataTextColumn myCol = new GridViewDataTextColumn();
        myCol.Caption = "col1";
        myCol.FieldName = "dataTableField1";
        myCol.DataItemTemplate = new ColumnDataItemTemplate();
        theGridView.Columns.Clear();
        theGridView.Columns.Add(myCol);
        theGridView.DataSource = AdjustDataSource();
        theGridView.DataBind();
        ...
    }

    public class ColumnDataItemTemplate : ITemplate
    {
        public void InstantiateIn(Control container)
        {
            GridViewDataItemTemplateContainer Container = (container as GridViewDataItemTemplateContainer);
            LiteralControl lit = new LiteralControl("<div id=\"hr\" style=\"height:100%\"><%#Eval(\"dataTableField1\")%></div>");
            Container.Controls.Add(lit);
        }
    }

Here is an example of what I want to do with the row height taken from this link:

<dx:GridViewDataTextColumn FieldName="Description" VisibleIndex="3">
    <DataItemTemplate>
        <div id="hr" style="height:100%">
            <%#Eval("Description")%>
        </div>
    </DataItemTemplate>
</dx:GridViewDataTextColumn>

This documentation link shows similar examples of using templates in the markup but I want to do it in the code behind.

Here is a link on creating templates.

Thanks in advance,

Soenhay

Edit: I was able to get the first element to properly display by moving the template assignment to the CustomColumnDisplayText event but all other elements displayed in the ASPxGridView show the Eval string.

도움이 되었습니까?

해결책

I had 2 other solutions but I removed them as I think this is the best ( I would be grateful for any other suggestions/improvements ):

        public override void DataBind()
        {
             ...
             GridViewDataTextColumn myCol = new GridViewDataTextColumn();
             myCol.Caption = "col1";
             myCol.FieldName = "dataTableField1";
             myCol.DataItemTemplate = new ColumnDataItemTemplate();
             theGridView.Columns.Clear();
             theGridView.Columns.Add(myCol);
             theGridView.DataSource = AdjustDataSource();
             theGridView.DataBind();
             ...
        }

        public class ColumnDataItemTemplate : ITemplate
        {
            public void InstantiateIn(Control container)
            {
                GridViewDataItemTemplateContainer Container = (container as GridViewDataItemTemplateContainer);
                LiteralControl lit = new LiteralControl("<div id='hr' style='height:100%; font-size:x-large;'>" + DataBinder.Eval(Container.DataItem, Container.Column.FieldName) + "</div>");
                Container.Controls.Add(lit);
            }
        }

At this link I have found a reason to not use the event: "The CustomColumnDisplayText event should not be handled for template columns. "

This link helped with the DataBinder.Eval part.

다른 팁

Heres an example but mine is binding with a database, i usually use EditTemplte

</EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="BSA" SortExpression="BSA">
                <ItemTemplate>
                    <asp:Label ID="lblBSA" runat="server" Text='<%# Bind("BSA") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top