Grid.CurrentViewData method always returns null

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. However when I use the .CurrentViewData of any of the grids in the dictionary, it returns a null.


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 ===================
}



3 Replies

MS Monisha Saravanan Syncfusion Team January 24, 2022 01:12 PM UTC

Hi Scott, 

Greetings from Syncfusion support. 

Query: I access the grid referenced in the @ref attribute and I use the grid.CurrentViewData method, the resulting grid object is null. 

We understand your requirement and we suggest you to use the dictionary items directly to the reference while rendering instead of using methods. We had modified the sample directly to access the mygrid variable. 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>; 
        } 
    } 
 
} 


Kindly get back to us if you have further queries. 

Regards, 
Monisha 



SC Scott January 24, 2022 07:06 PM UTC

Magic!

That worked! 

Thank you for your help. :-)



VN Vignesh Natarajan Syncfusion Team January 25, 2022 03:48 AM UTC

Hi Scott,  

Thanks for the update.  

We are glad to hear that you have resolved your query using our solution.  

Please get back to us if you have further queries. 

Regards, 
Vignesh Natarajan 


Loader.
Up arrow icon