Set Initially Selected Rows in a Grouped Grid

Hey  There,

I have a grouped Grid:  

            <SfGrid @ref="sfGridView" DataSource="@allElements" AllowGrouping="true" AllowPaging="true" AllowSorting="true" AllowTextWrap="true" AllowFiltering="false">
                <GridPageSettings PageSize="20" />
                <GridGroupSettings ShowDropArea="false" Columns="@InitialGrouping"></GridGroupSettings>
                <GridColumns>
                    <GridColumn Type="ColumnType.CheckBox" Width="40" ></GridColumn>

                     ......................

                    </GridColumn>
                </GridColumns>
            </SfGrid>
        </Content>

And I'm trying to set the initially selected rows using the example here: 
https://blazor.syncfusion.com/documentation/datagrid/selection/#multiple-selection-based-on-condition

However, this line throws an exception:

                    var records = await sfGridView.GetCurrentViewRecords();

System.InvalidCastException: Unable to cast object of type 'Syncfusion.Blazor.Data.Group`1[Models.LearningElement]' to type 'Models.LearningElement'.
   at System.Linq.Enumerable.CastIterator[TResult](IEnumerable source)+MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Syncfusion.Blazor.Grids.SfGrid`1.GetCurrentViewRecords()
   at SpotOn.Base.SelectLearningElementDialogComponent.OnAfterRender(Boolean firstRender) 
   in Web.Solution\Blazor\Shared\Dialogs\SelectLearningElementDialog.razor.cs:line 144
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__139_0(Object state)
   at Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.ExecuteSynchronously(TaskCompletionSource`1 completion, SendOrPostCallback d, Object state)
   at Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.<>c.<.cctor>b__23_0(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.ExecuteBackground(WorkItem item)


7 Replies 1 reply marked as answer

RN Rahul Narayanasamy Syncfusion Team August 25, 2020 03:09 PM UTC

Hi Joseph, 

Greetings from Syncfusion. 
      
We have your query and we have confirmed it is a bug and logged the defect report “Exception throws while calling GetCurrentViewRecords method in grouped Grid”. Thank you for taking the time to report this issue and helping us improve our product. At Syncfusion, we are committed to fixing all validated defects (subject to technological feasibility and Product Development Life Cycle ) and including the defect fix in our upcoming  bi-weekly release which is expected to be rolled out on or before end of September, 2020.     
    
You can now track the current status of your request, review the proposed resolution timeline, and contact us for any further inquiries through this link.    
    
    
Until then we appreciate your patience.     
  
Regards,  
Rahul 



JT Joseph Tan replied to Rahul Narayanasamy August 27, 2020 12:05 AM UTC

So, what's the best way to pre-select records before rendering, when the grid is grouped? 


RN Rahul Narayanasamy Syncfusion Team August 27, 2020 11:55 AM UTC

Hi Joseph, 

Query: So, what's the best way to pre-select records before rendering, when the grid is grouped? 

We have validated your and you want to preselect the rows at initial rendering of the Grid. You can achieve your requirement by using DataBound event and SelectRows method of the Grid. Here, we have selected row index’s(0,2,5) at initial rendering of the Grid. Find the below code snippets and sample for your reference. 

 
@{ 
    var Initial = (new string[] { "OrderID" }); 
} 
 
<SfGrid @ref="@Grid" DataSource="@Orders" AllowGrouping="true" AllowFiltering="true" AllowPaging="true" Height="315"> 
    <GridSelectionSettings Type="SelectionType.Multiple"></GridSelectionSettings> 
    <GridGroupSettings Columns="@Initial"></GridGroupSettings> 
    <GridEvents DataBound="Data" TValue="Order"></GridEvents> 
    <GridColumns> 
        . . . 
    </GridColumns> 
</SfGrid> 
 
@code{ 
 
    SfGrid<Order> Grid; 
    public List<Order> Orders { get; set; } 
    double[] index = { 0, 2, 5 };   //row index 
 
    protected override void OnInitialized() 
    { 
        . . . 
    } 
 
    public async Task Data(object args) 
    { 
        await Grid.SelectRows(index)//select rows. Here, you need to pass array of row index’s which needs to be selected  
    } 
} 


Reference

Please get back to us if you need further assistance. 

Regards, 
Rahul 


Marked as answer

JT Joseph Tan August 29, 2020 12:59 AM UTC

That worked - thanks


RN Rahul Narayanasamy Syncfusion Team August 31, 2020 05:09 AM UTC

Hi Joseph, 
 
Thanks for the update. 
 
We are happy to hear that the provided solution was helpful to achieve your requirement. 
 
Please get back to us if you need further assistance. 
 
Regards, 
Rahul 



JT Joseph Tan September 1, 2020 10:27 PM UTC

Hi,

There is a problem though - if the number of items in the grid (e.g. 50) is larger than the paging size (e.g. 20), and the selected item index is not in the first page (e.g. 21) then an exception is thrown, and none of the items are selected.

[10:25:13 ERR] SelectElementDialogComponent.GridDataBoundEvent() Exception : Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

Is there a way to also pre-select items which are outside  the viewable area?

Thanks
Joseph




RN Rahul Narayanasamy Syncfusion Team September 2, 2020 03:40 PM UTC

Hi Joseph, 

We have validated your query with provided information and you want to select the row(using SelectRows method) by using the row index which is not present in the current view. Hence the issue occurs. We can achieve your requirement by using GetCurrentViewRecords method. 

But currently we are having problem with GetCurrentViewRecords method with grouped grid. Once the issue fixed, we can achieve this requirement. Until then we appreciate your patience. 

Regards, 
Rahul 




Loader.
Up arrow icon