3.0/LINQ [RESOLVED] Binding a LINQ result to a data grid-VBForums
Results 1 to 3 of 3

Thread: [RESOLVED] Binding a LINQ result to a data grid

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    249

    Resolved [RESOLVED] Binding a LINQ result to a data grid

    Hi all, i have a grid view control bound to a linq query result, with a boolean property to select the row (check box)

    here is my code
    Code:
    class WB
        {
             [ReadOnly(false)]
         public bool selected {get;set;}
            [ReadOnly(true)]
         public string CusCode {get;set;}
            [ReadOnly(true)]
         public string CusTitle {get;set;}
            [ReadOnly(true)]
         public string  OrderNum {get;set;}
            [ReadOnly(true)]
         public string  Product {get;set;}
            [ReadOnly(true)]
         public int?  LocationNum {get;set;}
         public string  Alias {get;set;}
            [ReadOnly(true)]
         public decimal?  Qty { get; set; }
    }
    ant the Linq
    Code:
     IEnumerable<WB> q;
     q= from L in DBcontext.ADV_Logs
                        join TH in DBcontext.TRANSHEADERS on L.TransactionID equals TH.TransactionID
                        join P1 in DBcontext.PRODUCTS on L.PrdID equals P1.UniqueID
                        join P2 in DBcontext.PRODUCTS on L.AliasID equals P2.UniqueID
                        join C in DBcontext.CUSTOMERS on TH.AccountID equals C.UniqueID
                        select new WB
                        {
                            selected = false,
                            CusCode = C.CustomerCode,
                            CusTitle = C.CustomerTitle,
                            OrderNum = TH.AccountingRef,
                            Product = P1.ProductCode,
                            LocationNum = L.Loc,
                            Alias = P2.ProductCode,
                            Qty = L.QtyOffset
                        };
    
                         
                MainGrd.DataSource = q;
                GRD.PopulateColumns();
    WHen i tick the box i excpect the underlaying data to be updated (updates property selected to true ), but it is not for a reason that i cant work out !!
    i even tried this, message box displays falase for all rows !
    Code:
      foreach (WB r in q)
                {
                    r.selected = true;
                }
                foreach (var r in q)
                {
                    MessageBox.Show(r.selected.ToString());
                }

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,063

    Re: Binding a LINQ result to a data grid

    First you say that you have a "data grid" and then you have a "grid view". What do you actually have? Every control has a name and only one name. If you give us that actual name then we'll know what it is and not have to guess and maybe guess wrong. If it is, as I suspect, a DataGridView, then I would have thought that that code would fail anyway. A DataGridView requires an IList to bind and you're only providing an IEnumerable.

    Regardless, you need to understand how LINQ works. Your query, i.e. `q` is NOT a list of data. It is an object that can be enumerated to get a list of data. Every time you enumerate it, the LINQ query is executed. That means that you're getting a list of new WB objects every time. Changing any of those WB objects won't affect the WB objects that you get next time you enumerate it.

    If you want to evaluate the query and get a single list that you can then use, you need to call ToArray or ToList on the query. That will give you an array or generic List and you then use that list, NOT the query, again later. Both the array and generic List implement the IList interface and are therefore appropriate to bind to a DataGridView.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    249

    Re: Binding a LINQ result to a data grid

    Legend,, Thank You

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.