2

Basically, what I want to do is to bind a Linq query to the datasource of a gridview. Here is my code so far :

ddgDossiers.DataSource = (From c In dbConnection.Campaigns.AsQueryable Select c).ToList
ddgDossiers.DataBind()

It compiles and doesn't complain of anything.

<asp:GridView ID="ddgDossiers" runat="server" CellPadding="0" CellSpacing="0" AllowSorting="True"
AutoGenerateColumns="False" EnableViewState="True" AllowPaging="True" PageSize="35"
PagerStyle-HorizontalAlign="Right" PagerStyle-Mode="NumericPages" PagerStyle-Position="TopAndBottom"
BorderWidth="1" GridLines="Both" BorderColor="#000000" CssClass="mGrid" PagerStyle-CssClass="pgr">
<PagerSettings Position="TopAndBottom" />
<Columns>
    <asp:TemplateField Visible="false" HeaderText="UniqueID" ItemStyle-HorizontalAlign="left"
        HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="1%" HeaderStyle-Width="1%"
        HeaderStyle-CssClass="GridTitle" ItemStyle-CssClass="rowReport" SortExpression="UniqueId">
        <ItemTemplate>
            <asp:Label ID="lblUniqueID" runat="server" Text='<%#Container.DataItem("Campaign.CampaignId").ToString%>' />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>

The error is pointing on this line :

<asp:Label ID="lblUniqueID" runat="server" Text='<%#Container.DataItem("CampaignId").ToString%>' />

And saying : No default member found for type 'Campaign'.

Is there anything to do with that? Thanks in advance.

3
  • You shouldn't need to do the .ToList to bind it. Does Campaigns table contain a CampaignId column? Commented Sep 17, 2013 at 22:10
  • Try Removing Campaign. and just use CampaignId <%#Container.DataItem("CampaignId") Commented Sep 17, 2013 at 22:23
  • My bad. It was a test. I did already with CampaignId only. And yes the table contains this field. Commented Sep 18, 2013 at 13:08

1 Answer 1

2

change line

Text='<%#Container.DataItem("Campaign.CampaignId").ToString%>'

To

Text='<%# DataBinder.Eval(Container.DataItem, "CampaignId") %>' 
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I needed. Thanks a lot!!!! Works like a charm. I would rate up your answer, but I do not have enough rep yet ^^.

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.