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

entity framework with 2 gridviews

4 Answers 86 Views
GridView
This is a migrated thread and some comments may be shown as answers.
florian
Top achievements
Rank 1
florian asked on 24 Nov 2017, 03:37 PM

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

 

4 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 27 Nov 2017, 12:37 PM
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.
0
florian
Top achievements
Rank 1
answered on 27 Nov 2017, 02:16 PM

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

 

 

0
Accepted
Hristo
Telerik team
answered on 28 Nov 2017, 01:24 PM
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.
0
florian
Top achievements
Rank 1
answered on 08 Dec 2017, 10:06 AM
Thank you, my problem is now solved (sorry for the late response from myself!)
Tags
GridView
Asked by
florian
Top achievements
Rank 1
Answers by
Hristo
Telerik team
florian
Top achievements
Rank 1
Share this question
or