user2149152
user2149152

Reputation: 65

ASP.NET GridView TemplateField Call function(:int)

My scenario is the following:

I have a GridView and made a class the represents a row in it. I was expecting to show the properties and function's result of the class in the cells.

For this i'm using columns like this:

<asp:TemplateField HeaderText="1">
    <ItemTemplate>
        <asp:Label runat="server" text='<%# MatchesCount((Int32)1) %>' />
    </ItemTemplate>
</asp:TemplateField>

the class has the following function:

public class MatchesGridViewRow
{
...
    public string MatchesCount(int day) {...}
}

and I bind to the GridView like this:

GridView.DataSource = GetGridViewData(DateTime.Now.Month);
GridViewCalendar.DataBind();    

private List<MatchesGridViewRow> GetGridViewData(int month);

The error that I get is: CS0103: The name 'MatchesCount' doesnt exist in current context.

Isnt this the right way to call the method? If not how should i call it? Thanks from now, looking forware to answers.

Upvotes: 3

Views: 7132

Answers (2)

Harry Sarshogh
Harry Sarshogh

Reputation: 2197

i check below code and it useful : i hope

 <asp:TemplateField HeaderText="type" ItemStyle-Width="200">
                    <ItemTemplate>
                        <asp:Label ID="lbtype" runat="server" Text='<%# GetDescrptionHumanType(Convert.ToInt32(Eval("HumanType"))) %>' ></asp:Label>
                    </ItemTemplate>
                    <ItemStyle Width="200px"></ItemStyle>
                </asp:TemplateField>

   public  static string GetDescrptionHumanType(int val)
    {
       return  Utility.EnumEx.GetEnumDescription((HumanResourceType)val);

    }


   public static string GetEnumDescription(Enum value)
    {
        FieldInfo fi = value.GetType().GetField(value.ToString());

        DescriptionAttribute[] attributes =
            (DescriptionAttribute[])fi.GetCustomAttributes(
            typeof(DescriptionAttribute),
            false);

        if (attributes != null &&
            attributes.Length > 0)
            return attributes[0].Description;
        else
            return value.ToString();
    }

Upvotes: 1

user1193035
user1193035

Reputation:

Try this for change the method to static method

public static  string MatchesCount(int day) {...}

then call

'<%# MatchesGridViewRow.MatchesCount((Int32)1)%>'

Please check this same issue

Accessing public static class files from .ASPX file using Eval("") from gridView

Upvotes: 3

Related Questions