entity framework with 2 gridviews

5 posts, 1 answers
  1. florian
    florian avatar
    7 posts
    Member since:
    Nov 2017

    Posted 24 Nov Link to this post

    Hello, 

    I have a radgridview with a datasource linked to entity framework and it works fine.

    I have an entity called "USER" and I display all my users in the radgridview, it works just fine.

    In my USER entity, I have an attribute "GROUP" which is a collection :

    public virtual ICollection<GROUP> GROUP { get; set; }

     

    I would like to add a second radgridview to display groups details when you select a user. I think I have to play with event SelectionChanged on the main radgridview but I don't really know how to handle this.

     

    Thanks, 

    Florian

     

  2. Hristo
    Admin
    Hristo avatar
    1087 posts

    Posted 27 Nov Link to this post

    Hi Florian,

    Thank you for writing.

    It looks like you are having a scenario which is suitable for creating a hierarchical RadGridView. We have an example demonstrating how you can use Database First approach and show the data in the grid. Additionally, you can check the following KB article using Code First approach, it has data models similar to the ones you describe. The actual binding of the templates is at the end of the article.

    In case using a hierarchical grid is not suitable and you would like to display the child data associated with each record in a separate RadGridView you can handle the CurrentRowChanged event. Additional information about the selected and current row is available here: https://docs.telerik.com/devtools/winforms/gridview/rows/selected-rows-and-current-row

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. florian
    florian avatar
    7 posts
    Member since:
    Nov 2017

    Posted 27 Nov in reply to Hristo Link to this post

    Thanks for your response.

    Your second solution is the best for my case but I still have some difficulties.

    I load my main gridview to display all users :

    radGridViewUserManagement.DataSource = _entities.T_USER.Local.ToBindingList();

    It works fine, I can insert/update/delete rows and save to my database with _entities.SaveChanges();

    When I handle the CurrentRowChanged event :

     private void radGridViewUserManagement_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
            {
                if (e.CurrentRow != null && e.CurrentRow is GridViewDataRowInfo)
                {
                    currentUser = (T_USER) radGridViewUserManagement.CurrentRow.DataBoundItem;
                    radGridViewSelectedGroups.DataSource = currentUser.T_GROUP.ToList();
                }
            }

    I can display my current user Groups, but the view is never updated if I add some groups programmatically with currentUser.T_GROUP.Add(testGroup);

    I think my problem is the using of ToList() function when I set the DataSource.

    I tried many things but I cannot update the radGridViewSelectedGroups.

     

    Thanks for your help Hristo

     

     

  4. Answer
    Hristo
    Admin
    Hristo avatar
    1087 posts

    Posted 28 Nov Link to this post

    Hi Florian,

    Thank you for writing back.

    It appears that the grid is not receiving a notification that you have changed its data source collection. If that is really the case it will not update automatically. You can try using a BindingList just as shown in the documentation article
    dbContext.Customers.Load();
    this.radGridView1.DataSource = this.dbContext.Customers.Local.ToBindingList();

    I hope this helps. Let me know if you have other questions.

    Regards,
    Hristo
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  5. florian
    florian avatar
    7 posts
    Member since:
    Nov 2017

    Posted 08 Dec in reply to Hristo Link to this post

    Thank you, my problem is now solved (sorry for the late response from myself!)
Back to Top