Deselecting a row in a grid from code

I would like to deselect one row from the code. I only found oportunity to clear all selection with this:

await Grid10.ClearRowSelectionAsync();

But my problem with it that it clears all selection which I would not want. Just a one with a specific index.

Is there any way it can be done?

Thanks for your help!


1 Reply

PS Prathap Senthil Syncfusion Team August 16, 2024 10:18 AM UTC

Hi Nandor Kalmanchey,

We would like to inform you that the ClearRowSelectionAsync method can be used to clear all selected rows in the DataGrid. If you want to clear the selection of only a specific record, we suggest using the GetSelectedRowIndexesAsync() method to retrieve the selected row indexes. You can then use the ClearRowSelectionAsync method to clear the selection. From the list of selected row indexes, you can remove the specific index of the row you want to deselect. Afterward, you can reselect rows programmatically using the SelectRowsAsync method to achieve the desired outcome. Please refer to the code snippet and sample below for more details.

<span>@Message</span>

 

<Syncfusion.Blazor.Buttons.SfButton OnClick="@(async () => await ClearRowSelection(1))">

    DeSelectRow1

</Syncfusion.Blazor.Buttons.SfButton>

<SfGrid DataSource="@Orders" @ref="Grid" AllowSelection="true" AllowPaging="true">

    <GridSelectionSettings Type="Syncfusion.Blazor.Grids.SelectionType.Multiple"></GridSelectionSettings>

---------------

</SfGrid>

@code {

 

    public string Message { get; set; }

    SfGrid<Order> Grid { get; set; }

 

 

    public  async Task ClearRowSelection(int index)

    {

        List<int> selectedIndexes = await Grid.GetSelectedRowIndexesAsync();

 

        if (selectedIndexes != null && selectedIndexes.Contains(index))

        {

            Message = "";

            // Use LINQ to remove the specific index

            int[] selectedarray = selectedIndexes.Where(i => i != index).ToArray();

 

            await Grid.ClearSelectionAsync();

         

            await Grid.SelectRowsAsync(selectedarray);

 

        }

        else

        {

            Message = $"The given row index {index} is not currently selected.";

        }

    }

 

 

 

 

}


Sample:
https://blazorplayground.syncfusion.com/embed/VZrfNlidBzdTPJoE?appbar=true&editor=true&result=true&errorlist=true&theme=bootstrap5


Reference:
https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.SfGrid-1.html#Syncfusion_Blazor_Grids_SfGrid_1_ClearRowSelectionAsync
https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.SfGrid-1.html#Syncfusion_Blazor_Grids_SfGrid_1_GetSelectedRowIndexesAsync

https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.SfGrid-1.html#Syncfusion_Blazor_Grids_SfGrid_1_ClearRowSelectionAsync



Regards,
Prathap Senthil


Loader.
Up arrow icon