开发者

GridView did not have any properties or attributes from with to generate columns

开发者 https://www.devze.com 2023-02-07 02:57 出处:网络
This is the oddest error I\'ve ever encountered as it makes no sense to.I\'m getting an IList from my controller.I can even write a foreach loop and append TicketId to a stringbuilder and display my r

This is the oddest error I've ever encountered as it makes no sense to. I'm getting an IList from my controller. I can even write a foreach loop and append TicketId to a stringbuilder and display my results.

I can put in break points and see that I have the expected results returned....as EXPECTED! All values in place (no-misspellings as I've even copy & pasted)

But if I try to bind it to a GridView or repeater I get the error: The data source for GridView with id 'gv' did not have any properties or attributes from which to generate columns

example - in this code If I comment out the DataBinding to the gridview gv - I get the results as of the TicketId's as epxected:

LeadController controller = new LeadController();
        var leads = controller.SearchProducts(DateTime.Now.AddDays(-50),
                                                       DateTime.Now, string.Empty, string.Empty);

        var sb = new StringBuilder();

        foreach (LeadDto lead in (leads))
        {
            sb.Append(string.Format("{0} -----", lead.TicketId));
        }
        lblTop.Text = sb.ToString();

        gv.DataSource = leads;
        gv.DataBind();

But if the DataBinding isn't commented - the whole page errors with the above error. Here's my ascx

<asp:TextBox ID="lb开发者_StackOverflow社区lTop" runat="server" /><asp:GridView ID="gv" runat="server" AutoGenerateColumns="true" ></asp:GridView>

If I change my gridview as so:

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" >
<Columns>
    <asp:TemplateField >
        <ItemTemplate>
            <%# Eval("TicketId") %>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

I get an error that says: DataBinding: 'DataTransferObjects.LeadDto' does not contain a property with the name 'TicketId'.

But I can see it DOES after setting DataSource and prior to DataBinding...

I've tried shutting down VS2008 & even restarted my PC just in case it was a magic fairy bug....

Please, any suggestions?


You said the LeadDto object has a property named TicketId, but are you sure it's a property and not a field? That is, in your LeadDto class how is TicketId defined? If it's defined as a member variable then it is not a property:

public class LeadDto
{
     public int TicketId;

     ...
}

The databinding only can bind against properties:

public class LeadDto
{
     public int TicketId { get; set; };

     ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消