1

Html Code:

<asp:DataGrid ID="myGrid">
 <Columns>
    <asp:BoundColumn DataField="CustomerName" HeaderText="Customer">
       <ItemStyle Font-Bold="True" />
    </asp:BoundColumn>
 </Columns>
</asp:DataGrid>

Code Behind:

public string TestFunction(string str)
{
    return str.replace("A","B");
}

how to call TestFunction ? DataField="TestFunction(CustomerName)"

Best Regards...

2 Answers 2

2

You need to do this in a TemplateColumn:

<asp:DataGrid ID="myGrid">
 <Columns>
    <TemplateColumn>
        <ItemTemplate>
            <%# TestFunction(Eval("CustomerName") as string) %>
        </ItemTemplate>
    </TemplateColumn>
 </Columns>
</asp:DataGrid>

This however means you will loose autosorting and editing - but who uses that anyway :)

Sign up to request clarification or add additional context in comments.

3 Comments

You shouldn't lose autosorting and editing if you are bound to a DataView and you create an appropriate EditItemTemplate.
i want to make BoundColumn ? am i make ?
@Oraclee if you want to put it in a BoundColumn you have to add it as a property to your datasource @Joel Etherton EditItemTemplate => true, however sorting wont work IIRC
0
DataField='<%# TestFunction((string)Eval("MasterDesc")) %>'

UPDATE:

http://forums.asp.net/t/996979.aspx

2 Comments

= Databinding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.BoundColumn does not have a DataBinding event
Yes, per veggerby, you need to have it in a template?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.