Hide checkboxes in rows where they are not required

Hello,

I'm using the SfDataGrid control, and one of my columns is of type GridCheckBoxColumn.
There are some rows that I do not want to show the checkbox for, based on criteria in other columns.

For example, if Column1 is of type GridCheckBoxColumn, Column2 is SalesPersonName and Column3 is SalesInDollars, how can I hide the checkbox if SalesInDollars is less than 100,000?

I'm using C# and WinForms.

Thanks.

5 Replies 1 reply marked as answer

MA Mohanram Anbukkarasu Syncfusion Team October 30, 2020 10:42 AM UTC

Hi Brent, 

Thanks for contacting Syncfusion support.  

You can achieve your requirement to hide checkbox of a GridCheckBoxColumn based on the value of another column value in the same row by creating a custom renderer for the GridCheckBoxColumn as shown in the following code example. 

Code example :  

this.sfDataGrid1.CellRenderers["CheckBox"] = new CustomCheckBoxCellRenderer(); 
 
public class CustomCheckBoxCellRenderer : GridCheckBoxCellRenderer 
{ 
    protected override void OnRender(Graphics paint, Rectangle cellRect, string cellValue, CellStyleInfo style, DataColumnBase column, RowColumnIndex rowColumnIndex) 
    { 
        DataRowBase dataRow = (DataRowBase)column.GetType().GetProperty("DataRow", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(column); 
        if ((dataRow.RowData as SalesDetails).SalesInDollars < 100000) 
            return; 
 
        base.OnRender(paint, cellRect, cellValue, style, column, rowColumnIndex); 
    } 
} 

We have prepare a sample using the above code example to replicate your scenario and it is available in the following link for your reference.  


 

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

Regards, 
Mohanram A. 


Marked as answer

ST Stoked October 30, 2020 04:10 PM UTC

Thank you. The provided code works.

To take this one step further, is there a way the resulting cell can be painted without the checkbox so that the background of the cell is consistent with the remainder of the table?
I feel that the "return;" statement doesn't allow this. I would like to have the cell match all other cell characteristics in styling, color, etc.

Perhaps another "base.OnRender" can be done instead of "return;" fulfilling this request?

Please let me know if this is possible.


MA Mohanram Anbukkarasu Syncfusion Team November 2, 2020 01:21 PM UTC

Hi Brent, 

Thanks for the update.  

You can resolve this by modifying the custom render as shown in the following code example.  

Code example :  

public class CustomCheckBoxCellRenderer : GridCheckBoxCellRenderer 
{ 
    protected override void OnRender(Graphics paint, Rectangle cellRect, string cellValue, CellStyleInfo style, DataColumnBase column, RowColumnIndex rowColumnIndex) 
    { 
        DataRowBase dataRow = (DataRowBase)column.GetType().GetProperty("DataRow", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(column); 
 
        if ((dataRow.RowData as SalesDetails).SalesInDollars < 100000) 
            paint.FillRectangle(new SolidBrush(style.BackColor), cellRect); 
        else 
            base.OnRender(paint, cellRect, cellValue, style, column, rowColumnIndex); 
    } 
} 


 


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

Regards, 
Mohanram A. 




ST Stoked November 2, 2020 05:52 PM UTC

Beautiful, works like charm.


MA Mohanram Anbukkarasu Syncfusion Team November 3, 2020 09:26 AM UTC

Hi Brent,  

Thanks for the update.   

We are glad to know that the provided solution worked at your end. Please let us know if you have any further queries on this. We are happy to help you.  

Regards,  
Mohanram A. 


Loader.
Up arrow icon