How to disable Checkbox editing based on another column value

I have used CurrentCellBeginEdit event to disable text box based on the value of a cell like this
private void dataGridOwners_CurrentCellBeginEdit(object sender, CurrentCellBeginEditEventArgs e)
        {
            if(e.Column.MappingName== "OwnerName")
            {
                var rowIndex = e.RowColumnIndex.RowIndex;
                var recordIndex = this.dataGridOwners.ResolveToRecordIndex(rowIndex);
                var record = this.dataGridOwners.View.Records[recordIndex].Data as Owner;

                if(record.OwnerName == "TBD")
                {
                    e.Cancel = true;
                }
            }
}

However when I use the same method for checkbox:
if (e.Column.MappingName == "OwnerValid")
            {
                var rowIndex = e.RowColumnIndex.RowIndex;
                var recordIndex = this.dataGridOwners.ResolveToRecordIndex(rowIndex);
                var record = this.dataGridOwners.View.Records[recordIndex].Data as Owner;

                if (record.OwnerName == "TBD")
                {
                    e.Cancel = true;
                    
                }
            }
It does not cancel the edit of a check box.
Is there another way to do it?


3 Replies 1 reply marked as answer

MA Mohanram Anbukkarasu Syncfusion Team March 12, 2021 05:41 AM UTC

Hi Mariusz Juszkiewicz,  
   
Thank you for contacting Syncfusion support.  
   
By default, editing related events is not triggered for GridCheckBoxColumn in SfDataGrid. However you can disable the editing the checkbox value specific cells based on other column values by creating a custom renderer for the checkbox column as shown in the following code example.  

Code example :  

this.dataGrid.CellRenderers["CheckBox"] = new CustomGridCellCheckBoxRenderer(); 
 
public class CustomGridCellCheckBoxRenderer : GridCellCheckBoxRenderer 
 { 
     public override void OnInitializeEditElement(DataColumnBase dataColumn, CheckBox uiElement, object dataContext) 
     { 
         base.OnInitializeEditElement(dataColumn, uiElement, dataContext); 
 
         var record = dataContext as OrderInfo; 
         if (record.CustomerID == "FRANS") 
             uiElement.IsEnabled = false; 
     } 
 } 


Please let us know if you require further assistance from us.  

Regards, 
Mohanram A. 


Marked as answer

MJ Mariusz Juszkiewicz March 14, 2021 02:58 AM UTC

Mohanram,

This worked great. Thank you!
I have another dilemma. I have two columns in SfDataGrid one is a checkboxColumn and another GridDateTimeColumn.
What I need to do is Enable GridDateTimeColumn based on value of the checkboxColumn. When GridCheckBoxColumn is checked
I need to Disable (or prevent editing) and null the value of GridDateTimeColum.
Hope you can help me with this?


MA Mohanram Anbukkarasu Syncfusion Team March 15, 2021 07:32 AM UTC

Hi Mariusz Juszkiewicz,   

Thanks for the update.  

You can disable editing for a column based on the value of another column value in same row by using CurrentCellBeginEdit event as  shown in the following code example.  

Code example :  

private void DataGrid_CurrentCellBeginEdit(object sender, CurrentCellBeginEditEventArgs e) 
 { 
     if(e.Column.MappingName == "OrderDate") 
     { 
         var dataRow = this.dataGrid.RowGenerator.Items.FirstOrDefault(item => item.RowIndex == e.RowColumnIndex.RowIndex); 
 
         if ((dataRow.RowData as OrderInfo).IsDelivered) 
         { 
             e.Cancel = true; 
         } 
     } 
 } 



Please let us know if you require further assistance from us.  

Regards, 
Mohanram A. 


Loader.
Up arrow icon