How to condtionaly remove buttons from a cell

WIthin a SfDataGrid, when a column is defined in code as a GridButtonColumn, how can the buttons be removed from a specific cell when a condition is satisfied?  I have tried handling the QueryCellStyle, QueryRowStyle, and QueryButtonStyle events, but I have not been able to determine a process that would remove the buttons.  In the DrawCell event, every attempt appears to remove the buttons from the entire column.  The data source of the Grid is an IEnumerable<DataRecord>.

The Grid Buttons are added to the grid in code similar to the following:

List<CellButton> buttons = [];
 buttons.AddRange(
     new CellButton() { Text = ClientConstant.Buttons.ADD_NEW },
     new CellButton() { Text = ClientConstant.Buttons.MODIFY_EXISTING },
     new CellButton() { Text = ClientConstant.Buttons.REMOVE_EXISTING }
     );
dataGrid.Columns.Add(new GridButtonColumn()
{
    MappingName = ClientConstant.Grids.ACTION,
    HeaderText = ClientConstant.Grids.ACTION,
    Buttons = buttons,
    Orientation = Orientation.Horizontal,
    AutoSizeColumnsMode = AutoSizeColumnsMode.AllCells,
});
private void GrdPayments_QueryRowStyle(object sender, QueryRowStyleEventArgs e)
{
    if (e.RowType.Equals(RowType.DefaultRow))
    {
        if ((e.RowData as ParcelPaymentInformationResponse)!.RemovedOn.HasValue)
        {
            e.Style.BackColor = ColorHelper.FromHtml(SyncfusionConstant.ColorName.WINFORM_WARNING);
        }
    }
}
private void GrdPayments_DrawCell(object sender, DrawCellEventArgs e)
{
    if (e.Column.MappingName.Equals(ClientConstant.Grids.ACTION))
    {
        if ((e.DataRow.RowData as ParcelPaymentInformationResponse)!.RemovedOn.HasValue)
        {
        }
    }
}

4 Replies

RM Rabina Murugan Syncfusion Team June 30, 2025 02:18 PM UTC

Hi Greg,


As you mentioned, If you remove the buttons in Drawcell event it will be removed in the entire column and it is a behavior. As analyzed in the possible ways, SfDatagrid does not have support to remove the button from the specific cell in GridButtonColumn. However you can disable the button in the specific cell. This can be achieved by enabling and disabling in the QueryButtonCellStyle event.


Code snippet to disable button in QueryButtonCellStyle event:

public Form1()

{

 InitializeComponent();

 this.sfDataGrid1.QueryButtonCellStyle += OnSfDataGrid_QueryButtonCellStyle;

}

 

private void OnSfDataGrid_QueryButtonCellStyle(object sender, Syncfusion.WinForms.DataGrid.Events.QueryButtonCellStyleEventArgs e)

{

 if (e.Button.Text == "Button2" && e.RowIndex % 3 == 0)

 {

 e.Style.Enabled = false;

 }

}


Image illustration of disabled buttons in specific cells:

GridButton.png

Refer to the following knowledge base article for more information and sample demo,

KB link: How to enable/disable specific button in WinForms DataGrid?


Please let us know if you need any further assistance.


Regards,

Rabina M


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



GR Greg June 30, 2025 03:27 PM UTC

I have been able to disable the buttons with the QueryButtonCellStyle event and that works fine.  I was hoping for a method that would remove the buttons.  

Would it be possible to submit an enhancement request to add a "Visible" property to the ButtonCellStyleInfo class?



SB Sweatha Bharathi Syncfusion Team July 1, 2025 03:10 PM UTC

Hi Greg, 

Currently, we do not have support to remove a button from a specific cell in the GridButtonColumn based on certain conditions. However, we have analyzed and considered your requirement and have logged a feature request for the same.

At the planning stage for every release cycle, we review all open features and identify features for implementation based on specific parameters including product vision, technological feasibility, and customer interest.

We will provide the feature feedback link by tomorrow.

Note: In the meantime, you can disable the button in the specific cell as per the previously suggested solution.

Regards,
Sweatha B


SB Sweatha Bharathi Syncfusion Team July 2, 2025 06:39 AM UTC

Hi Greg, 

Currently we do not have support to remove a button from a specific cell in the GridButtonColumn based on certain conditions. We have analyzed and considered your requirement of "Provide support for changing the visibility of the button in GridButtonColumnand logged a feature request for the same. We will implement this feature in any of our upcoming releases.
 
At the planning stage for every release cycle, we review all open features and identify features for implementation based on specific parameters including product vision, technological feasibility, and customer interest. We will let you know when this feature is implemented. We appreciate your patience until then.
 
Thank you for requesting this feature and helping us define it. We are always trying to make our products better and feature requests like yours are a key part of our product growth efforts.
 
 
If you have any more specifications/suggestions for the feature request, you can add them as a comment in the portal and cast your vote to make it count.


Regards,
Sweatha B

Loader.
Up arrow icon