Category / Section
How to disable the context menu for the textbox in edit mode in WinForms DataGrid (SfDatGrid)?
2 mins read
The ContextMenu can be disabled for a TextBox in edit mode by deriving a new class from GridTextBoxCellRenderer and overriding the OnWireEditUIElement while assigning an empty context menu into the ContextMenu property of TextBox.
public partial class Form1 : Form { public Form1() { InitializeComponent(); //Remove existing TextBox Renderer sfDataGrid.CellRenderers.Remove("TextBox"); //Add customized TextBox Renderer sfDataGrid.CellRenderers.Add("TextBox", new GridTextBoxCellRendererExt()); } } public class GridTextBoxCellRendererExt : GridTextBoxCellRenderer { protected override void OnWireEditUIElement(TextBox uiElement) { base.OnWireEditUIElement(uiElement); //To prevent the default context menu of a TextBox from showing up, assign a empty context menu as shown below uiElement.ContextMenu = new ContextMenu(); } }
Note:
This can also be done for GridNumericColumn by deriving a new class from GridNumericCellRenderer.
Take a moment to peruse the WinForms DataGrid – Customize Column Renderer documentation, where you can find about customize column renderer with code examples.