Articles in this section
Category / Section

How to show the context menu while adding a new record using addNewRow in WinForms DataGrid?

1 min read

While adding a new records in WinForms DataGrid (SfDataGrid) does not provide the direct support to showing Context Menu in AddNewRow. You can achieve your requirement by customizing 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);
        }
    }
}

Shows the context menu in AddNewRow


The following screenshot shows the ContextMenu in AddNewRow in DataGrid,


Shows the context menu in AddNewRow of SfDataGrid

Take a moment to peruse the WinForms DataGrid - Context Menu documentation, where you can find about context menu with code examples.

View sample in GitHub

Conclusion

I hope you enjoyed learning about how to show the context menu in AddNewRow of DataGrid.

You can refer to our WinForms DataGrid  feature tour page to know about its other groundbreaking feature representations, WinForms DataGrid documentation and how to quickly get started for configuration specifications.  You can also explore our WinForms DataGrid  example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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