Load on Demand a Hierarchial Relationship with Entity Frame work

3 posts, 0 answers
  1. Tim
    Tim avatar
    15 posts
    Member since:
    Jul 2010

    Posted 13 Feb 2015 Link to this post

    I have a simple code first database that I want to load on demand within a hierarchial grid. I have everythign working except I don't know how to cast the results within the RowSourceNeed procedure.
    public class Product
        {
            public int ProductId { get; set; }
            public string Name { get; set; }
     
            public int CategoryId { get; set; }
            public virtual Category Category { get; set; }
        }
     
        public class Category
        {
            private readonly ObservableListSource<Product> _products =
                    new ObservableListSource<Product>();
     
            public int CategoryId { get; set; }
            public string Name { get; set; }
            public virtual ObservableListSource<Product> Products { get { return _products; } }
        }
     
        public class ProductContext : DbContext
        {
            public DbSet<Category> Categories { get; set; }
            public DbSet<Product> Products { get; set; }
     
            public ProductContext()
                : base("my conn string")
            {
                this.Configuration.AutoDetectChangesEnabled = true;
            }


    I setup the child template thusly:
    this.radGridView2.RowSourceNeeded += new Telerik.WinControls.UI.GridViewRowSourceNeededEventHandler(this.radGridView2_RowSourceNeeded);
     
                GridViewTemplate childTemplate = new GridViewTemplate();
                childTemplate.HierarchyDataProvider = new GridViewEventDataProvider(childTemplate);
                childTemplate.DataSource = this.productsBindingSource;
                childTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
                radGridView2.MasterTemplate.Templates.Add(childTemplate);


    Then in the radGridView2_RowSourceNeeded  event when I open the row

    private void radGridView2_RowSourceNeeded(object sender, GridViewRowSourceNeededEventArgs e)
    {      e.ParentRow.DataBoundItem as   ??? }

    I can see when I run it that e.ParentRow.DataBoundItem is a System.Data.Entity.DynamicProxies.Category_485959... type

    See attached.

    and that if I could just access the.Products of it then I could iterate and add the rows.

    Any ideas how to access? Thanks.







  2. Tim
    Tim avatar
    15 posts
    Member since:
    Jul 2010

    Posted 16 Feb 2015 in reply to Tim Link to this post

    I think I made it more difficult than needed. Just needed to cast as the Class I was expecting.

    private void radGridView2_RowSourceNeeded(object sender, GridViewRowSourceNeededEventArgs e)
            {
                var cats = e.ParentRow.DataBoundItem as Model_Pipeline.Category;
                 
                foreach (var  product in cats.Products)
                {
                    GridViewRowInfo row = e.Template.Rows.NewRow();
                    row.Cells["ProductId"].Value = product.ProductId;
                    row.Cells["Name"].Value = product.Name;
                    row.Cells["CategoryId"].Value = product.CategoryId;
                    row.Cells["Category"].Value = product.Category;
     
                    e.SourceCollection.Add(row);
                }
            }

               
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    712 posts

    Posted 18 Feb 2015 Link to this post

    Hello Tim,

    Thank you for writing. 

    I am glad that you found a working solution. Thank you for sharing it with our community.

    An example of binding a hierarchical grid to data with a load on demand feature can be found here.

    Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top