I am displaying information in multiple grids on a page.
I am creating multiple grids as items in a dictionary and I'm attempting to use each item as the value of the @ref attribute for each 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.
Here is my code.
@foreach(var item in items_to_display)
<SfGrid @ref=grid DataSource="@Items" AllowTextWrap="true" AllowSelection="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="SOQItemModel" 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="10px" Field=@nameof(Item.Item) HeaderText="ITEM"></GridColumn>
<GridColumn Width="120px" Field=@nameof(Item.Desc) HeaderText="DESCRIPTION"> </GridColumn>
</GridColumns>
</SfGrid>
The myGrids dictionary is populated with the grids as expected. Intellisense in VS lets me know that each grid is of type SfGrid
private Dictionary<string, SfGrid<SOQItemModel>> myGrids = new Dictionary<string, SfGrid<SOQItemModel>>();public SfGrid<Item> get_mygrid(string gridName)
{
if(!(myGrids.Keys.Contains(gridName)))
myGrids.Add(gridName,newSfGrid- ());
return myGrids[gridName];
}
I use the following code to try to access the grids, however it is always null.
foreach(var gridkey in myGrids.Keys)
{
var grid = myGrids[gridkey].CurrentViewData as IEnumerable<object>;
//=========grid is null here ===================
}
|
<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>;
}
}
} |
Magic!
That worked!
Thank you for your help. :-)