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)
{
}
}
}
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:
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.
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?
- 4 Replies
- 3 Participants
-
GR Greg
- Jun 29, 2025 03:49 PM UTC
- Jul 2, 2025 06:39 AM UTC