GridCheckBoxSelectorColumn select only on clik in this colum

I have added GridCheckBoxSelectorColumn to the SfDataGridfield. How can I ensure that row selection only occurs when I click some row only column that contains the GridCheckBoxSelectorColumn on ohter column.


3 Replies

MS Malini Selvarasu Syncfusion Team October 21, 2024 01:51 PM UTC

Hi Krzysztof,

Based on the information provided, we understand that you would like the row selection to occur exclusively when the `GridCheckBoxSelectorColumn` is clicked. If any other column is clicked, the selection should not take place. This can be accomplished by utilizing the `SelectionChanging` event. In the event handler, you can verify whether the currently clicked column is the `GridCheckBoxSelector` column. If it is not, you can cancel the event arguments to prevent the selection from occurring in other columns.
To assist you further, we have prepared a sample demonstrating this approach. Please review the sample along with the accompanying code snippet and let us know if it meets your requirements.
Code Snippet:
this.sfDataGrid1.SelectionChanging += SfDataGrid1_SelectionChanging;
 private void SfDataGrid1_SelectionChanging(object sender, Syncfusion.WinForms.DataGrid.Events.SelectionChangingEventArgs e)
{
    if(!(this.sfDataGrid1.CurrentCell.Column is GridCheckBoxSelectorColumn))
    {
         e.Cancel = true;
    }
}

If there is any misunderstanding regarding your requirements, please provide more details so we can assist you further. Thank you for your understanding and cooperation.

Regards,
Malini Selvarasu

Attachment: SfDataGrid_Demo_d0d9e929.zip


KR Krzysztof January 18, 2025 05:46 PM UTC

Your solution works well, but there's an issue when I have the CheckBoxOnHeader added and click on it, then it doesn't work correctly.



MS Malini Selvarasu Syncfusion Team January 20, 2025 01:45 PM UTC

Hi Krzysztof,

Based on the information provided, we understand that the selection functionality did not work correctly after enabling the `AllowCheckBoxOnHeader` property. This issue occurred because the previous implementation only checked the current cell value of the row data. As a result, when clicking the HeaderCheckBox, the condition was incorrectly evaluated as true, causing `e.Cancel` to be set to true and preventing selection.
To resolve this, we have updated the implementation to retrieve the `RowColumnIndex` and determine the column using the column index. We then modified the condition to ensure that selection is canceled only if the column is not a `GridCheckBoxSelectorColumn`. This adjustment ensures the selection works as expected. Please refer to the modified sample and the accompanying code snippet below.
Code Snippet:
private void SfDataGrid1_SelectionChanging(object sender, Syncfusion.WinForms.DataGrid.Events.SelectionChangingEventArgs e)
{
     var point = sfDataGrid1.PointToClient(Cursor.Position);
     rowColumnIndex = sfDataGrid1.TableControl.PointToCellRowColumnIndex(point);
     var column = sfDataGrid1.Columns[rowColumnIndex.ColumnIndex];
     if ( column!= null && !(column is GridCheckBoxSelectorColumn)) 
     { 
         e.Cancel = true;
     }
}
Thank you for your cooperation and understanding.
Regards,
Malini Selvarasu

 


Attachment: SfDataGrid_Demo_c21bdb1f.zip

Loader.
Up arrow icon