I have a simple use of the Grid Component:
<SfGrid DataSource="@BagList)" AllowSorting="true" AllowFiltering="true" AllowSelection="true">
<GridSelectionSettings Type="SelectionType.Single" Mode="SelectionMode.Row"></GridSelectionSettings>
<GridEvents RowSelected="GetSelectedRecords" TValue="Data.BagView"></GridEvents>
<GridColumns>
<GridColumn Field=@nameof(Data.BagView.ModelName) HeaderText="Model"></GridColumn>
<GridColumn Field=@nameof(Data.BagView.StyleName) HeaderText="Style"></GridColumn>
<GridColumn Field=@nameof(Data.BagView.SizeName) HeaderText="Size"></GridColumn>
<GridColumn Field=@nameof(Data.BagView.FabricName) HeaderText="Fabric"></GridColumn>
<GridColumn Field=@nameof(Data.BagView.ColorName) HeaderText="Color"></GridColumn>
<GridColumn Field=@nameof(Data.BagView.SeasonName) HeaderText="Season"></GridColumn>
</GridColumns>
</SfGrid>
@code {
IList<Data.BagView> BagList = new List<Data.BagView>();
SfGrid<Data.BagView> Grid;
public List<double> SelectedRowIndexes { get; set; }
protected override async Task OnInitializedAsync()
{
base.OnInitialized();
using (var ctx = ContextFactory.CreateDbContext())
{
string sqlQuery = @"---sql query to fill the BagList list comes here---";
BagList = ctx.BagViews.FromSqlRaw(sqlQuery).ToList();
}
}
public async Task GetSelectedRecords(RowSelectEventArgs<Data.BagView> args)
{
SelectedRowIndexes = await this.Grid.GetSelectedRowIndexes(); <=========== exception is here!
SelectedValue = SelectedRowIndexes[0];
StateHasChanged();
}
When I run the code the Grid gets created and everything is smooth but I click on a row to select it, it triggers GetSelectedRecords as it should but I get an error:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
<>4__this.Grid was null.
it is strange because the code is straight from Syncfusion page.