How to show the context menu while adding a new record using AddNewRow in WinForms DataGrid (SfDataGrid)?
This example illustrates how to show the context menu while adding a new record using AddNewRow in WinForms DataGrid (SfDataGrid)?
WinForms DataGrid (SfDataGrid) does not provide the direct support to showing Context Menu in AddNewRow. You can achieve your requirement by customization the MouseUp event in SfDataGrid.TableControl.
sfDataGrid.TableControl.MouseUp += SfDataGrid_MouseUp;
//Get the record context while AddingNewRow menu by customization
private void SfDataGrid_MouseUp(object sender, MouseEventArgs e)
{
// get the row and column index based on the pointer position
var rowColIndex = sfDataGrid.TableControl.PointToCellRowColumnIndex(e.Location);
//Check the condition is AddNewRow and Only show the context menu while pressing right button of Mouse
if (e.Button == MouseButtons.Right && sfDataGrid.IsAddNewRowIndex(rowColIndex.RowIndex) && this.sfDataGrid.RecordContextMenu != null && !rowColIndex.IsEmpty)
{
ContextMenuStrip contextMenu = null;
//set the Record contextMenu for AddNewRow
contextMenu = this.sfDataGrid.RecordContextMenu;
//get the location
var location = this.sfDataGrid.TableControl.PointToScreen(e.Location);
if (contextMenu != null)
{
//show the ContextMenu for AddNewRow
contextMenu.Show(location);
}
}
}
The following screenshot shows the ContextMenu in AddNewRow in WinForms DataGrid (SfDataGrid),
Take a moment to peruse the WinForms DataGrid - Context Menu documentation, where you can find about Context Menu with code examples.
Please refer this link to know about the essential features of WinForms DataGrid.
Visual Studio 2015 and above versions