英文:
TelerikGrid async datasource
问题
我试图从数据库异步获取数据来设置网格的数据,但它不起作用。可能我漏掉了一些东西。
@page "/Test"
@using System.Dynamic;
<div class="col-md-12">
<TelerikButton OnClick="ButtonClick">点击我以查看数据</TelerikButton>
<TelerikGrid Class="grid"
Data=@GridFilter
Sortable="true"
FilterMode="GridFilterMode.FilterMenu"
Resizable="true"
SelectionMode="GridSelectionMode.Multiple"
Pageable="true"
PageSize=20
Navigable="true">
<GridColumns>
<GridColumn Field="Field1" Title="Field1" />
<GridColumn Field="Field2" Width="300px" Title="Field2" TextAlign="@ColumnTextAlign.Center" />
</GridColumns>
</TelerikGrid>
</div>
@code {
private List<dynamic> GridFilter;
private async Task ButtonClick()
{
GridFilter = await this.GetData();
}
private async Task<List<dynamic>> GetData()
{
Task<List<dynamic>> b = new(() => {
dynamic A = new ExpandoObject();
A.Field1 = "Field 1";
A.Field2 = "Field 2";
List<dynamic> grid = new();
grid.Add(A);
return grid;
});
return await b;
}
}
希望这有助于您解决问题。
英文:
I'm trying to set data of grid by fetching data asynchronously from DB but it is not working. Probably I'm missing something.
@page "/Test"
@using System.Dynamic;
<div class="col-md-12">
<TelerikButton OnClick="ButtonClick">Click Me to see data</TelerikButton>
<TelerikGrid Class="grid"
Data=@GridFilter
Sortable="true"
FilterMode="GridFilterMode.FilterMenu"
Resizable="true"
SelectionMode="GridSelectionMode.Multiple"
Pageable="true"
PageSize=20
Navigable="true">
<GridColumns>
<GridColumn Field="Field1" Title="Field1" />
<GridColumn Field="Field2" Width="300px" Title="Field2" TextAlign="@ColumnTextAlign.Center" />
</GridColumns>
</TelerikGrid>
</div>
}
@code {
private List<dynamic> GridFilter;
private async Task ButtonClick()
{
GridFilter = await this.GetData();
}
private async Task<List<dynamic>> GetData()
{
Task<List<dynamic>> b = new(() => {
dynamic A = new ExpandoObject();
A.Field1 = "Field 1";
A.Field2 = "Field 2";
List<dynamic> grid = new();
grid.Add(A);
return grid;
});
return await b;
}
}
答案1
得分: 1
根据此文档,如果您希望在更新列表时使网格自动更新,您必须将集合声明为ObservableCollection
。您可能还需要初始化一个空集合,以便网格可以订阅其事件:
private ObservableCollection<dynamic> GridFilter = new ObservableCollection<dynamic>();
但这意味着您不能用新的集合实例替换GridFilter
,就像您在这里所做的一样:
GridFilter = await this.GetData();
您必须将每个项目添加到现有集合中。您可以在ButtonClick()
事件中执行此操作,或者直接在GetData()
中将项目添加到GridFilter
中。
不幸的是,ObservableCollection
不支持AddRange()
,所以您必须一个一个添加。或者,您可以使用某人编写的ObservableRangeCollection
类,使其支持AddRange
。
另一种方法是在更改集合后调用网格上的Rebind方法,可能看起来像这样(未经测试,可能需要一些调整):
<TelerikGrid Class="grid"
Data=@GridFilter
Sortable="true"
FilterMode="GridFilterMode.FilterMenu"
Resizable="true"
SelectionMode="GridSelectionMode.Multiple"
Pageable="true"
PageSize=20
Navigable="true"
@ref="@GridRef">
private async Task ButtonClick()
{
GridFilter = await this.GetData();
GridRef.Rebind();
}
英文:
Based on this documentation, if you want the grid to automatically update when you update the list, you have to declare the collection as an ObservableCollection
. You'll likely also have to initialize an empty collection so that the grid can subscribe to its events:
private ObservableCollection<dynamic> GridFilter = new ObservableCollection<dynamic>();
But that means you cannot replace GridFilter
with a new instance of a collection, like you're doing here:
GridFilter = await this.GetData();
You'll have to add each item to the existing collection. You can either do that in your ButtonClick()
event, or add the items directly to GridFilter
in GetData()
.
ObservableCollection
doesn't support AddRange()
unfortunately, so you'll have to add one at a time. Or, you can use this ObservableRangeCollection
class someone wrote to make it support AddRange
.
The other way is to call the Rebind method on the grid after you change the collection, which might look something like this (untested, so it might need some tweaks):
<TelerikGrid Class="grid"
Data=@GridFilter
Sortable="true"
FilterMode="GridFilterMode.FilterMenu"
Resizable="true"
SelectionMode="GridSelectionMode.Multiple"
Pageable="true"
PageSize=20
Navigable="true"
@ref="@GridRef">
private async Task ButtonClick()
{
GridFilter = await this.GetData();
GridRef.Rebind();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论