RadEntityFrameworkCoreDataSource and RadGridView

1 Answer 53 Views
GridView
Lee
Top achievements
Rank 1
Veteran
Lee asked on 15 Dec 2022, 07:58 PM

Hello,

I'm trying to set up a RadGridView to display a RadEntityFrameworkCoreDataSource source using code-behind (currently a requirement) and cannot get the grid to populate with any data. I can access the data by using LINQ just fine, and can populate the grid using a ToList() version of that data, but I believe that it's just a copy of the data, and cannot be modified and saved to the original context.

Here's an example of what I'm trying to do:

var options = new DbContextOptionsBuilder<Test_Context>();
options.UseSqlServer(ConnectionString);
using var testContext= new Test_Context(options.Options);

RadEntityFrameworkCoreDataSource source = new()
{
    Name = "coreDataSource",
    DbContext = testContext,
    QueryName = "TblCustomer"
};

// This doesn't populate the grid
rgvCustomers.ItemsSource = source.DataView;
                
// This populates the grid
var customerList = textContext.TblCustomer.Where(customer => customer.Id != -1).ToList();
rgvCustomers.ItemsSource = customerList ;

How do I get the RadEntityFrameworkCoreDataSource to query the data so that it can populate the grid?

Also, how would I go about only querying the customers with an Id != -1 using said Data Source? Is that as simple as setting the filters, then querying?

I'm new to this, so apologies if I've missed a simple step.

1 Answer, 1 is accepted

Sort by
1
Accepted
Dilyan Traykov
Telerik team
answered on 20 Dec 2022, 12:17 PM

Hello Lee,

Thank you very much for the provided code snippet.

The RadEntityFrameworkCoreDataSource control is actually meant to be used in XAML for simplicity and the logic to fetch the underlying data is called in its Loaded event which does not get invoked in this case.

To load the data, you need to add the control to the visual tree. For example, if the RadGridView control is placed in a Grid with x:Name set to "LayoutRoot", you can add the following line to your code to correctly load the data:

var options = new DbContextOptionsBuilder<Test_Context>();
options.UseSqlServer(ConnectionString);
using var testContext= new Test_Context(options.Options);

RadEntityFrameworkCoreDataSource source = new()
{
    Name = "coreDataSource",
    DbContext = testContext,
    QueryName = "TblCustomer"
};

this.LayoutRoot.Children.Add(source);
rgvCustomers.ItemsSource = source.DataView;

Alternatively, you can use the generic QueryableEntityCoreCollectionView class which is specifically designed for MVVM and code-behind scenarios:

            var view = new QueryableEntityCoreCollectionView<Customer>(testContext, testContext.Customers, new Collection<string>() { "Orders" });
            rgvCustomers.ItemsSource = view;

A good example of its use can be found in the following article: MVVM Usage.

As for applying a filter to the query, you can use the FilterDescriptors collection of both the RadEntityFrameworkCoreDataSource and the QueryableEntityCoreCollectionView like so:

source.FilterDescriptors.Add(new FilterDescriptor("Id", FilterOperator.IsNotEqualTo, -1));

Please give this a try and let me know if you're now able to display the data as expected.

Regards,
Dilyan Traykov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Lee
Top achievements
Rank 1
Veteran
commented on 23 Dec 2022, 06:50 PM

Hi Dilyan,

Thanks for the explanation. I wasn't able to add the source to the tree via your method, but I suspect it's due to my setup. The QueryableEntityCoreCollectionView approach, and the filtering, worked fantastically.

Thanks,

Lee

Tags
GridView
Asked by
Lee
Top achievements
Rank 1
Veteran
Answers by
Dilyan Traykov
Telerik team
Share this question
or