views:

25

answers:

2

Hi;

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  A: 

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 :)

veggerby
@veggerby - You shouldn't lose autosorting and editing if you are bound to a DataView and you create an appropriate EditItemTemplate.
Joel Etherton
@veggerby i want to make BoundColumn ? am i make ?
Oraclee
@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
veggerby
A: 
DataField='<%# TestFunction((string)Eval("MasterDesc")) %>'

UPDATE:

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

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