views:

786

answers:

3

I currently have a Gridview that displays

TypeID , Name , Description.

I would like to display the actual type name instead of the TypeID in the gridview. I created this function that takes in the ID and returns the Name but I am having trouble using it. There are 15-20 different types so How do I convert the TypeID to a Type Name so that it is displayed when the Gridview is rendered.

protected string GetGenericTypeByID(int genericTypeID)
        {
            string genericTypeName;

            GenericType.Generic_TypeDataTable genericTypeNameDS = new GenericType.Generic_TypeDataTable();
            genericTypeNameDS = GenericBO.Get_GenericTypeByID(genericTypeID);

            genericTypeName = genericTypeNameDS[0]["Generic_Type_Name"].ToString();









            return genericTypeName;

        }

I thought I would be able to use the function in the ItemTemplate but it seems to be harder that I thought

 <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("GetGenericTypeByID("Generic_Type_ID")")%>'></asp:Label>
                </ItemTemplate>

Thanks to Everyone who helped me solve this problem. I ended up using the method below and it works perfectly. GetGenericTypeByID( Convert.ToInt32(Eval("Generic_Type_ID")))

A: 

Create a read-only property on the row class that you're using to populate the grid, and get this property to return the results of your function.

Chris
This will only work if you're databinding to something like a List<T> where T is your row class. If you're databinding to a sql data set, then this won't be much help.
Chris
+1  A: 

You've got the 'bind/eval' and method call inside out.
See Using Method inside a DataGrid or GridView TemplateField

<asp:TemplateField HeaderText=”Name”>
<ItemTemplate>
    <a href='<%# FormatUrl(Eval(”email1″).ToString())%>'><%# Eval(”fname”) %>,&nbsp;<%# Eval(”lname”) %></a>
</ItemTemplate>

With the 'FormatUrl' function being:

public string FormatUrl(string email)
{
    return “mailto:” + email;
}
David HAust
+1  A: 

Are you limited to a label tag? If not, Expanding on David HAust's answer try the following:

<ItemTemplate>
    <%#GetGenericTypeByID(Eval(Generic_Type_ID))%>
</ItemTemplate>
Jon P