简体   繁体   English

Blazor Syncfusion Grids:Grid.CurrentViewData 方法总是返回 null

[英]Blazor Syncfusion Grids: The Grid.CurrentViewData method always returns null

I am using Blazor Server and Syncfusion grids (SfGrid) to display information in multiple grids on a page.我正在使用 Blazor 服务器和 Syncfusion 网格 (SfGrid) 在页面上的多个网格中显示信息。

My issue is after I access the grid referenced in the @ref attribute and I use the grid.CurrentViewData method, the resulting grid object is null.我的问题是在我访问@ref 属性中引用的网格并使用grid.CurrentViewData方法后,生成的网格 object 是 null。

=====example workflow====== =====示例工作流程======

The user indicates which items to save by using a checkbox column for each grid.用户通过使用每个网格的复选框列来指示要保存的项目。

When I grab the grid during the save routine, to check which rows are checked, the grid is always null.当我在保存例程中抓取网格时,要检查检查了哪些行,网格始终是 null。

Here is my code.这是我的代码。

@foreach(var item in items_to_display) 
    <SfGrid @ref=get_mygrid(item.name) DataSource="@Items" AllowTextWrap="true"  
                        AllowSorting ="false" AllowFiltering="false" AllowPaging="true" Width="100%" 
                        EnableAltRow="true" > 
                    <GridTextWrapSettings WrapMode="WrapMode.Content"></GridTextWrapSettings> 
                    <GridPageSettings PageSize=10></GridPageSettings>
                    <GridSelectionSettings  CheckboxMode="CheckboxSelectionType.ResetOnRowClick" 
                                CheckboxOnly="false" PersistSelection="true"></GridSelectionSettings>
                    <GridEvents TValue="Item" OnActionFailure="@ActionFailure"></GridEvents>
                    <GridColumns>
                        <GridColumn Field=@nameof(Item.Selected) Width="10px" >
                            <Template>
                                @{
                                    var item = (context as Item);
                                    <SfCheckBox Checked="item?.Selected"></SfCheckBox>
                                }
                            </Template>
                        </GridColumn>
                        <GridColumn Width="30px" Field=@nameof(Item.Group) HeaderText="Section"></GridColumn>
                        <GridColumn Width="10px" Field=@nameof(Item.ItemName) HeaderText="ITEM"></GridColumn>
                        <GridColumn Width="120px" Field=@nameof(Item.Description)  HeaderText="DESCRIPTION"> </GridColumn> 
                    </GridColumns>
                </SfGrid> 
     

My grid objects used in the @ref attribute in the grids...我在网格中的@ref 属性中使用的网格对象...

private Dictionary<string, SfGrid<Items>> myGrids = new Dictionary<string, SfGrid<Items>>();
public SfGrid<Items> get_mygrid(string gridName)
{ 
    if (!(myGrids.Keys.Contains(gridName)) )
        myGrids.Add(gridName, new SfGrid<Item>()); 
    return myGrids[gridName];
}

I use the following code to try to access the grids, however it is always null.我使用以下代码尝试访问网格,但它始终是 null。

foreach(var gridkey in myGrids.Keys)
{ 
    var grid = myGrids[gridkey].CurrentViewData as IEnumerable<object>;
    //=========grid is null here ===================
 }

You can use the dictionary items directly to the reference while rendering instead of using methods.您可以在渲染时直接使用字典项来引用而不是使用方法。 We had modified the sample direct to access the mygrid variable.我们直接修改了示例以访问 mygrid 变量。 Kindly check the attached code snippet and sample for your reference.请检查随附的代码片段和示例以供参考。

<SfButton OnClick="clicked" Content="Click"></SfButton>

@foreach (var d in Data)

{

    <SfGrid DataSource="@Orders" @ref="myGrids[d]" AllowTextWrap="true" AllowSorting ="false" AllowFiltering="false" AllowPaging="true" Width="100%" EnableAltRow="true">

        <GridTextWrapSettings WrapMode="WrapMode.Content"></GridTextWrapSettings>

        <GridPageSettings PageSize=10></GridPageSettings>

        <GridColumns>

        </GridColumns>

    </SfGrid>

}

 

@code {

    public List<Order> Orders { get; set; }

    public List<string> Data = new List<string>() { "First", "second" };

    private Dictionary<string, SfGrid<Order>> myGrids = new Dictionary<string, SfGrid<Order>>();

 

    public void clicked()

    {

        foreach (var gridkey in myGrids.Keys)

        {

            var grid = myGrids[gridkey].CurrentViewData as IEnumerable<object>;

        }

    }

 

}

Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/DataGrid362229114.zip样品: https://www.syncfusion.com/downloads/support/directtrac/general/ze/DataGrid362229114.zip

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

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