我正在尝试使用类似于此示例的方法来创建一个动态表,在该表中我可以自己定义列。
症状是只打印标题,行为空。
我的观察结果是,在遍历数据行时,Columns
是空的。第二次测试显示,DataTable.AddColumn
调用发生在打印行之后,这解释了为什么行确实是空的(因为列是空的,所以没有什么可打印的!(
可以肯定的是,有一些逻辑缺失,以确保Columns
确实是在打印行之前添加的,但我无法发现。
魔术是添加OnAfterRender
(或其async
版本(以在第一次渲染时通知更改。。。
protected override void OnAfterRender(bool firstRender) {
if (firstRender) {
// The first render will instantiate the GridColumn defined in the ChildContent.
// GridColumn calls AddColumn during its initialization. This means that until
// the first render is completed, the columns collection is empty.
// Calling StateHasChanged() will re-render the component, so the second time it will know the columns
StateHasChanged();
}
}
感谢这篇文章。