Articles in this section
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.

View sample in GitHub

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied