SfGrid checkbox disabled with QueryCellHandle, but if you click on the select all option, the disabled checkbox is selected

Hi,


 I have this code, I use the function QueryCellInfoHandler for disable checkbox if DocVisualizado is false, but if I click in select all option, all rows are selected.


How to select all option ignore rows if data.DocVisualizado is false ?




Here is my razor:


<SfGrid @ref="gridDocumentos" DataSource="@todasSolicitãcoes" AllowPaging="true" AllowSorting="true">


    <GridTemplates>


        <EmptyRecordTemplate>


            <span>@emptyMessage</span>


        </EmptyRecordTemplate>


    </GridTemplates>


    <GridSelectionSettings Type="SelectionType.Multiple" ></GridSelectionSettings>


    <GridEvents RowSelected="GetDocumentosSelecionados" QueryCellInfo="QueryCellInfoHandler" TValue="SolicitacaoDocumentoInnerJoinDocumentosDTO"/>


    <GridPageSettings PageSize="12"></GridPageSettings>


    <GridColumns>


        <GridColumn Type="ColumnType.CheckBox" TextAlign="TextAlign.Center" HeaderTextAlign="TextAlign.Center" Width="2.5%"></GridColumn>


    </GridColumns>


</SfGrid>


And my code:


public void QueryCellInfoHandler(QueryCellInfoEventArgs<SolicitacaoDocumentoInnerJoinDocumentosDTO> Args)


{


    if (Args.Column.Type == ColumnType.CheckBox && !Args.Data.DocVisualizado)


    {


        Args.Cell.AddClass(new string[] { "e-checkbox-disabled" });


    }


}




public async Task GetDocumentosSelecionados(RowSelectEventArgs<SolicitacaoDocumentoInnerJoinDocumentosDTO> args)


{


    documentosSelecionados = await gridDocumentos.GetSelectedRecordsAsync();


}




I tried a lot, but nothing seems to work, please show me how to do it correctly.


Thanks.


3 Replies 1 reply marked as answer

PS Prathap Senthil Syncfusion Team February 2, 2024 12:04 PM UTC

Hi Gustavo,

Based on your requirements, to achieve the possible way after checking the selectAll option, retrieve the IsHeaderCheckboxClicked property from the RowSelectingEvent argument. To prevent the inbuilt selection, set args.Cancel to true and then customize the selection using the SelectRowsAsync method. When enabling PersistSelection, perform the selection using the databound event. Kindly refer to the below code snippet and sample for your reference.


<SfGrid DataSource="@Orders" @ref="Grid" Width="500" Height="300" TValue="Order" AllowSelection="true" AllowPaging="true">

 

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

    <GridEvents RowSelecting="RowSelectingHandler" DataBound="Data" TValue="Order"></GridEvents>
-------

</SfGrid>

@code {

    public List<Order> Orders { get; set; }

    SfGrid<Order>? Grid { get; set; }

    public List<int> RowIndexs { get; set; } = new List<int>();

    public bool flag { get; set; } = false;

    public bool isHeaderCheckbox { get; set; } = false;

    List<int> SelectIndex { get; set; }

 

    public async Task Data(object args)

    {

        if (isHeaderCheckbox && Grid.SelectionSettings.PersistSelection)

        {

 

            var Source = await Grid.GetCurrentViewRecords();

            SelectIndex = new List<int>();

            foreach (var record in Source)

            {

 

                if (record.Verified)

                {

                    var rowIndex = Grid.GetRowIndexByPrimaryKeyAsync(record.OrderID).Result;

                    SelectIndex.Add(rowIndex);

                }

            }

            await Grid.SelectRows(SelectIndex.ToArray());

        }

 

    }

 

    public async Task RowSelectingHandler(Syncfusion.Blazor.Grids.RowSelectingEventArgs<Order> args)

    {

        if (args.IsHeaderCheckboxClicked && !flag) //if header checkbox clicked

        {

            isHeaderCheckbox = args.IsHeaderCheckboxClicked;

            flag = true;

            args.Cancel = true;

            var len = args.RowIndexes.Count;

            for (int i = 0; i < len; i++)

            {

                if ((args.Datas[i].Verified))

                {

                    var value = args.Datas[i].OrderID;

                    var rowIndex = Grid.GetRowIndexByPrimaryKeyAsync(value);

                    int rvalue = Convert.ToInt32(rowIndex.Result);

                    RowIndexs.Add(rvalue); //add row index to list

                }

            }

            await Grid.SelectRowsAsync(RowIndexs.ToArray());//  select rows

            flag = false; //reset flag

        }

    }

}


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

Reference:
https://blazor.syncfusion.com/documentation/datagrid/events#databound

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

https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.SfGrid-1.html#Syncfusion_Blazor_Grids_SfGrid_1_SelectRowsAsync_System_Int32__
https://blazor.syncfusion.com/documentation/datagrid/selection#multiple-selection-based-on-condition

Regards,
Prathap S


Marked as answer

GH Gustavo Henrique Leles Coelho February 2, 2024 05:30 PM UTC

Hey Prathap Senthil, 

Just wanted to say a huge thanks for your lightning-fast help! Your solution worked like a charm. Really appreciate your speedy response and awesome support! 


Cheers, Gustavo.



PS Prathap Senthil Syncfusion Team February 5, 2024 07:54 AM UTC

Thanks for the update,


We are happy to hear that the provided solution was helpful. We are closing the thread now.


Loader.
Up arrow icon