How to implement mutual exclusivity in adjacent GridCheckBoxColumns?

I'm using the SfDataGrid control to re-create a legacy c++ application with 2 check boxes that only allow a single column to be checked in any row. 

E.g.20211103_120543.jpg

My SfDataGrid contains to GridCheckBoxColumns, but there doesn't appear to be a handler for the click event in the control itself. Should I be using a different column control in my grid, like custom column type? How do I implement this?


1 Reply

VS Vijayarasan Sivanandham Syncfusion Team November 8, 2021 02:22 PM UTC

Hi Zeljko Lazic,

Thank you for contacting Syncfusion Support.

Your requirement can be achieved by customization the Click event of CheckBox by override the OnInitializeEditElement method in GridCellCheckBoxRendererExt in SfDatGrid. Please refer the below code snippet,

 
this.sfDataGrid.CellRenderers.Remove("CheckBox"); 
this.sfDataGrid.CellRenderers.Add("CheckBox", new GridCellCheckBoxRendererExt()); 
 
 
public class GridCellCheckBoxRendererExt : GridCellCheckBoxRenderer 
{         
        public override void OnInitializeEditElement(DataColumnBase dataColumn, CheckBox uiElement, object dataContext) 
        {             
            base.OnInitializeEditElement(dataColumn, uiElement, dataContext); 
            uiElement.Click += OnClicked; 
        } 
 
        private void OnClicked(object sender, RoutedEventArgs e) 
        { 
            var checkBox = (e.OriginalSource as CheckBox); 
            var mappingName = (checkBox.Parent as GridCell).ColumnBase.GridColumn.MappingName; 
            //get the rowdata 
            var rowData = checkBox.DataContext; 
             
            //here customize based on your scenario 
            if (mappingName == "IsClosed") 
                (rowData as OrderInfo).IsSelected = !(bool)checkBox.IsChecked; 
             else if (mappingName == "IsSelected") 
                (rowData as OrderInfo).IsClosed = !(bool)checkBox.IsChecked; 
 
        } 
} 

Sample Link: https://www.syncfusion.com/downloads/support/forum/170148/ze/SfDataGridDemo-1571937760

For more information related to Customize column renderer, please refer the below user guide documentation link,

UG Link: https://help.syncfusion.com/wpf/datagrid/column-types#customize-column-renderer

Please let us know if you have any concerns in this.

Regards,
Vijayarasan S 


Loader.
Up arrow icon