We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to not select checkbox when open context menu on CheckBoxCell

I had a context menu opened when right clicking on a checkbox cell, and the value (true/false) of the cell will be applied to all rows of the particular GridCheckBoxColumn. However, when right clicking on the cell, the checkbox is also toggled. How can I disable the selection of checkbox and only show the context menu on right click?


1 Reply

VS Vijayarasan Sivanandham Syncfusion Team December 2, 2022 08:14 PM UTC

Hi Alison Tan,

Your requirement to disable the selection of checkbox and only show the context menu on the right click in SfDataGrid can be achieved by customizing the SelectionChanging, MouseClick, and CellCheckBoxClick events. Refer to the below code snippet,

//Event Subscription

sfDataGrid.SelectionChanging += OnSelectionChanging;

sfDataGrid.TableControl.MouseClick += OnMouseClick;

sfDataGrid.CellCheckBoxClick += OnCellCheckBoxClick;

 

 

//Event Customization

 private void OnCellCheckBoxClick(object sender, CellCheckBoxClickEventArgs e)

 {

     //Here customize based on your scenario

     if(isCheckBoxCellClicked)

     {

         //Here handle check or uncheck state of checkbox

         e.Cancel = true;

         //Here reset the boolean value

         isCheckBoxCellClicked = false;

     }

 }

 

 //here maintain the boolean value to only show the context menu on right click

 bool isCheckBoxCellClicked;

 

 

 private void OnMouseClick(object sender, MouseEventArgs e)

 {

     //Here customize based on your scenario

 

     // get the row and column index based on the pointer position

     var rowColIndex = sfDataGrid.TableControl.PointToCellRowColumnIndex(e.Location);

 

     //Check the condition is AddNewRow and Only show the context menu while pressing right button of Mouse

     if (e.Button == MouseButtons.Right && sfDataGrid.Columns[rowColIndex.ColumnIndex].MappingName == "IsShipped" && !rowColIndex.IsEmpty)

     {

         //here enable the boolean proeprty only for GridCheckboxColumn

         isCheckBoxCellClicked = true;

     }

 }

 

 private void OnSelectionChanging(object sender, SelectionChangingEventArgs e)

 {

     //Here customize based on your scenario

     if (isCheckBoxCellClicked)

     {

         //Here handle the Selection when right click on checkbox

         e.Cancel = true;              

     }

 }

 


For more information related to SelectionChanging, MouseClick, and CellCheckBoxClick events, please refer to the below user guide documentation link,


UG Link: https://help.syncfusion.com/windowsforms/datagrid/selection#cancel-selection

https://help.syncfusion.com/windowsforms/datagrid/columntypes#canceling-the-check-box-state-change

https://help.syncfusion.com/windowsforms/datagrid/gettingstarted#handling-events

KB Link: https://www.syncfusion.com/kb/12514/how-to-show-the-context-menu-while-adding-a-new-record-using-addnewrow-in-winforms-datagrid

Find the sample in the attachment.

Regards,

Vijayarasan S


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.



Attachment: SfDataGridDemo_d454ad25.zip

Loader.
Up arrow icon