This is a migrated thread and some comments may be shown as answers.

Load on Demand a Hierarchial Relationship with Entity Frame work

2 Answers 49 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 1
Tim asked on 13 Feb 2015, 07:41 PM
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 Answers, 1 is accepted

Sort by
0
Tim
Top achievements
Rank 1
answered on 16 Feb 2015, 08:44 PM
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);
            }
        }

           
0
Hristo
Telerik team
answered on 18 Feb 2015, 11:01 AM
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.

 
Tags
GridView
Asked by
Tim
Top achievements
Rank 1
Answers by
Tim
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or