简体   繁体   English

使用子集数据集 C# 重新加载 datagridview

[英]Reloading datagridview with a subset dataset C#

I have a GridView that is being populated by a subset .Select of Tables as below;我有一个 GridView 由下表的子集.Select填充,如下所示; then bound the list to the Grid View Data source.然后将列表绑定到网格视图数据源。

However, I found myself calling LoadCandidates() many times, any time a record is changed or even when adding a new record.但是,我发现自己多次调用LoadCandidates() ,无论何时更改记录,甚至添加新记录。 in order to refresh the Data Grid View, This means I have to query the database and then append it to the DGV Data Source every time a change happens.为了刷新数据网格视图,这意味着每次发生更改时我都必须查询数据库,然后将其 append 到 DGV 数据源。 to maintain the below DGV headers (subset).维护以下 DGV 标头(子集)。

is there a better way of implementing this?有没有更好的方法来实现这一点?

private async Task LoadCandidates()
{
            var candiatesList = await _dbContext.Candidates
            .Include(i => i.Candimmigration)
            .Select(s => new
            {
                Id = s.Id,
                FirstName = s.FirstName,
                LastName = s.LastName,
                DateOfBirth = s.DateOfBirth,
                VisaNo = s.Candimmigration.VisaNo,
                PassportNo = s.Candimmigration.PassportNo,
            })
            .ToListAsync();
            dgv.DataSource = candiatesList;
}

I believe you might want to implement Reactive behavior to your GridView.我相信您可能希望对您的GridView实施响应式行为。 This is achieved by Binding a UI element like your GridView to an Observable variable.这是通过将诸如 GridView 之类的 UI 元素绑定Observable变量来实现的。 Once you've made this binding, the grid will update itself every time the variable is set.完成此绑定后,每次设置变量时,网格都会自行更新。 There are many ways to do this and even frameworks that facilitate it.有很多方法可以做到这一点,甚至有促进它的框架。

Here is a good example of how to achieve this with regular.Net:这是一个很好的例子,说明如何使用 regular.Net 实现这一点:

https://docs.microsoft.com/en-us/dotnet/desktop/wpf/data/how-to-create-and-bind-to-an-observablecollection?view=netframeworkdesktop-4.8 https://docs.microsoft.com/en-us/dotnet/desktop/wpf/data/how-to-create-and-bind-to-an-observablecollection?view=netframeworkdesktop-4.8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM