GridView databinding to LINQ join query

Last post 11-23-2008 9:44 PM by Qin Dian Tang - MSFT. 3 replies.

Sort Posts:

  • GridView databinding to LINQ join query

    11-18-2008, 6:03 PM
    • Member
      157 point Member
    • rozialishah
    • Member since 07-16-2007, 7:18 AM
    • Posts 68

    Hi,

    I am having gridview which is binded to the linq query  as folllowing  

    Public Overridable Function getInventoryWorksheet(ByVal OrderDetailspredicates As List(Of Expression(Of Func(Of Order_Detail, Boolean))), _
                                                          Optional ByVal Productpredicates As List(Of Expression(Of Func(Of Product, Boolean))) = Nothing) As IQueryable
            ' These predicates will build the where condition for the query at run time
            Dim OrderDetailspredicate = PredicateBuilder.True(Of Order_Detail)()
            Dim Productpredicate = PredicateBuilder.True(Of Product)()
            ' Combine the predicates by And
            Productpredicate = PredicateBuilder.CombinePredicates_And(Productpredicates)
            OrderDetailspredicate = PredicateBuilder.CombinePredicates_And(OrderDetailspredicates)
            Dim db = New MyLinQDatabaseDataContext
            Dim orderDetails = From current In db.Order_Details.Where(OrderDetailspredicate)
            Dim groups = From mygroup In orderDetails.GroupBy(Function(group As Order_Detail) group.ItemID) Select New With _
                           { _
                            .ItemID = mygroup.Key, _
                            .QtyOrdered = mygroup.Sum(Function(group As Order_Detail) group.QTYOrdered), _
                            .QtyShipped = mygroup.Sum(Function(group As Order_Detail) group.QtyShiped) _
                           }
            ' Dim result = groups.Join(
            Dim groupdata = (From OD In groups Join Products In db.Products.Where(Productpredicate) On OD.ItemID Equals Products.SKU).DefaultIfEmpty
            'Dim datatable = groupdata.ToADOTable()
            'Dim stil = From testt In groupdata Select 
            Return groupdata
     End Function
    So my groupdata is Iquerable of two objects OD {ItemID,QtyOrdered,QtyShipped} and a complete Products object.
    I want to databind the gridview to groupdata Please see my code below 
     
     
      <asp:GridView ID="GvInventoryWorkSheet" AllowPaging="false"  AllowSorting="True"  width="800px"  
            PageSize="20"  AutoGenerateColumns="False"    
            runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" >
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
           <PagerStyle CssClass="PagerStyle" />
           <PagerSettings PageButtonCount="5" Position="TopAndBottom" Mode="NumericFirstLast"   FirstPageText="First" LastPageText="Last" />
            <RowStyle CssClass="customerListNormal" BackColor="#F7F6F3" 
                ForeColor="#333333" />
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="aspxCBX"   ToolTip='<%# DataBinder.Eval(Container.DataItem, "OD.ItemID") %>' runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
               <asp:BoundField  ItemStyle-Width="80px" HeaderText="Item No." DataField="OD.ItemID" SortExpression="OD.ItemID" />
               <asp:BoundField  ItemStyle-Width="80px" HeaderText="SKU" DataField="Products.SKU" SortExpression="Products.SKU" />
            </Columns>
            <PagerStyle  CssClass="GvPagerRow" />
            
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle CssClass="GvHeader" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle CssClass="customerlistAlternate" BackColor="White" 
                ForeColor="#284775" />
        </asp:GridView>
     
    But it say OD.ItemID is not a dataItem of the datasource. Can anybody tell me how can I acheive this functionality.
    Thank,
    Syed
     
    Syed Ali Shah

    ______________________________________________
    Attention: Don't Forget to mark as "Answer" If I helped you in solving your problem.
  • Re: GridView databinding to LINQ join query

    11-21-2008, 2:47 AM
    Qin Dian Tang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: GridView databinding to LINQ join query

    11-21-2008, 9:03 AM
    • Member
      157 point Member
    • rozialishah
    • Member since 07-16-2007, 7:18 AM
    • Posts 68

    Thanks you,

    It works the way the article explain but I was wondering if we could directly access object.property in the bound field of grid view.

    Syed

    Syed Ali Shah

    ______________________________________________
    Attention: Don't Forget to mark as "Answer" If I helped you in solving your problem.
  • Re: GridView databinding to LINQ join query

    11-23-2008, 9:44 PM

    Hi rozialishah,

    I think you can try to bind the property name directly in GridView. For me, if you bind any object such as List<>, I just use property name to bind the control. Not quite sure about your case, but just try it.

    Thanks,

    Qin Dian Tang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (4 items)