Datasource

Jul 23, 2008 at 4:19 PM
Is it possible to set the datasource of a gridview directly to an NLinq query? e.g.

 

Dim nlqQuery As New NLinqQuery("from u in users join n in notes on u.UserId equals n.UserId " & _
"select new { n.DateCreated, u.Name, n.Details }")
Dim nlqMem As New LinqToMemory(nlqQuery)
nlqMem.AddSource(
"users", colUsers)
nlqMem.AddSource(
"notes", colNotes)
Me.gvwNotes.DataSource = nlqMem. (?)

For a gridview with named columns, like:

 

 

<asp:GridView ID="gvwNotes" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="Date added" DataField="DateCreated" />
<asp:BoundField HeaderText="Author" DataField="Name" />
<asp:BoundField DataField="Details" />
</Columns>
</asp:GridView>

 

 

 

I've tried using .Enumerator() and .Enumerator(Of T) but the first claims that there is not a property called DateCreated on the datasource, and the second doesn't seem to know how to cast the nlinq variant into another collection type.
.List also doesn't seem to work by default. Any ideas?

Jul 24, 2008 at 10:21 AM
Well, I managed to get it working by casting the dataitem into a variant and then using the .Item property on it, but it would be nicer if we bind directly onto the collection

 

<asp:GridView ID="gvwNotes" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField HeaderText="Date added">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# CDate(CType(Container.DataItem, Evaluant.NLinq.Memory.Variant).Item("DateCreated")).ToString("dd/MM/yyyy") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Author">
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# CType(Container.DataItem, Evaluant.NLinq.Memory.Variant).Item("Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# CType(Container.DataItem, Evaluant.NLinq.Memory.Variant).Item("Details") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

 

Coordinator
Jul 25, 2008 at 8:43 AM
Edited Jul 25, 2008 at 8:46 AM
While searching for a convenient solution I discovered a syntax in the Eval method. Couldn't find any documentation, yet it works

<asp:TemplateField HeaderText="MethodName">

    <ItemTemplate>

        <%# Eval("[MethodName]") %>

    </ItemTemplate>

</asp:TemplateField>

 

<asp:TemplateField HeaderText="Overloads">

    <ItemTemplate>

        <%# Eval("[Overloads]") %>

    </ItemTemplate>

</asp:TemplateField>

Jul 25, 2008 at 9:39 AM
Yes, that works perfectly, thanks.

 

<asp:TemplateField HeaderText="Date added">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# CDate(Eval("[DateCreated]")).ToString("dd/MM/yyyy") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Author">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("[Name]") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("[Details]") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>