Grid.GetSelectedRecordsAsync Returns Null

Hi,

My grid has a checkbox column and I am attempting to gather all of the selected (checked) rows in my grid (SfGrid).

BTW all checkboxes are selected in all grids.

I know my grid object is populated as the Grid.CurrentViewData will return the visible rows in the grid.

I have tried the Grid.GetSelectedRecordsAsync and Grid.GetSelectedRowIndexesAsync methods but they return an empty list (0 records).


Returns all visible rows:

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


Returns an empty collection:

var rows =await myGrids[gridkey].GetSelectedRecordsAsync();


Any advice you can give for getting the selected rows in my grid?

Thanks in advance.:-)


3 Replies

RN Rahul Narayanasamy Syncfusion Team January 25, 2022 01:18 PM UTC

Hi Scott, 

Greetings from Syncfusion. 

Query: Grid.GetSelectedRecordsAsync Returns Null 

Based on the shared details, we have checked the reported problem. We could not able to reproduce the reported problem at our end. We can able to get the selected records of the Grid using GetSelectedRecordsAsync method. Find the below code snippets and sample for your reference. 

 
public async Task selected() 
    { 
        foreach (var gridkey in myGrids.Keys) 
        { 
            if(gridkey == "First") 
            { 
                var firstSelected = await myGrids[gridkey].GetSelectedRecordsAsync();    
            } else if (gridkey == "second") 
            { 
                var secondSelected = await myGrids[gridkey].GetSelectedRecordsAsync();    
            } 
        } 
    } 


 


If you are still facing the problem, then could you please share the below details. It will be helpful to validate and provide a better solution. 

  • Reproduce the problem in the provided sample and revert back to us.
  • Full Grid code snippets.
  • Share a simple reproduceable sample if possible.

Regards, 
Rahul 




SC Scott January 26, 2022 02:31 AM UTC

Hi,

I am able to reproduce the issue with the example you provided. You are correct that the .GetSelectedRecordAsync returns the selected row.

However I understand now that what I am really after, is to grab all of the checked rows, not necessarily the "selected" rows.

There are lots of rows in my grids and my users will likely never physically click on each row to select it.

I noticed that in the example you sent, there wasn't a checkbox column, so I added one to this example in the attachment.


How do I grab all of the checked rows in the grid?


If that is not possible directly, one solution would be if I could bind the checkbox column in the grid the to the Selected property of the data object, then I could iterate the data object and check the related Selected property.


Any help in finding the "checked" value for each row would be great! :-)


Thanks so much for sending the example, that is very useful in checking the issue on my end and being able to show you what I mean. :-)





Attachment: DataGrid_With_Checkbox_49abffe7.zip



RN Rahul Narayanasamy Syncfusion Team January 26, 2022 11:40 AM UTC

Hi Scott, 

Thanks for the update. 

Based on your shared details we have ensured the reported case at our end with the provided sample. You have rendered a checkbox using column template and checked that checkbox while rendering. So in this case, the rows in the Grid are not selected. Only the column template checkbox is selected alone. So while you are trying to get the selected records using GetSelectedRecordsAsync method, no records are returned. 

If you want to get the selected records, first you need to select the rows based on your Selected property in your Grid data source. After that you can get the selected records using the method.  

Here, we have modified your sample to achieve this case. We have selected the grid rows initially using DataBound event based on the Selected boolean property. So you can get the selected records while clicking the button click using GetSelectedRecordsAsync method. 

 
<SfButton OnClick="clicked" Content="Click"></SfButton> 
 
<SfButton OnClick="selected" Content="Get Selected"></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>  
        <GridSelectionSettings Type="Syncfusion.Blazor.Grids.SelectionType.Multiple"></GridSelectionSettings> 
        <GridEvents DataBound="DataBound" TValue="Order"></GridEvents> 
        <GridPageSettings PageSize=10></GridPageSettings> 
        <GridColumns> 
            <GridColumn Field=@nameof(Order.Selected) Width="10px" > 
                <Template> 
                    @{ 
                        var item = (context as Order); 
                        <SfCheckBox Checked="item?.Selected"></SfCheckBox> 
                    } 
                </Template> 
            </GridColumn> 
            . . . 
        </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 async Task selected() 
    { 
        foreach (var gridkey in myGrids.Keys) 
        { 
            if(gridkey == "First") 
            { 
                var firstSelected = await myGrids[gridkey].GetSelectedRecordsAsync();   
            } else if (gridkey == "second") 
            { 
                var secondSelected = await myGrids[gridkey].GetSelectedRecordsAsync();    
            } 
        } 
    } 
    List<double> SelectIndex { get; set; } 
    public async Task DataBound(object args) 
    { 
        foreach (var gridkey in myGrids.Keys) 
        { 
            if(gridkey == "First") 
            { 
                 var Source = await myGrids[gridkey].GetCurrentViewRecords(); 
                 var IndexNum = 0; 
                 SelectIndex = new List<double>(); 
                 foreach (var record in Source) 
                 { 
                     if (record.Selected) 
                     { 
                         SelectIndex.Add(IndexNum); 
                     } 
                     IndexNum++; 
                 } 
                 await myGrids[gridkey].SelectRows(SelectIndex.ToArray());   
            } else if (gridkey == "second") 
            { 
                 var Source = await myGrids[gridkey].GetCurrentViewRecords(); 
                 . . . 
                 await myGrids[gridkey].SelectRows(SelectIndex.ToArray()); 
            } 
        } 
        
    } 
 
    . ..  
 
    public class Order 
    { 
. . . 
        public bool Selected { get; set; } = true; 
    } 
} 


Please let us know if you have any concerns. 

Regards, 
Rahul 


Loader.
Up arrow icon