This documentation is archived and is not being maintained.

DataGridViewRowContextMenuStripNeededEventArgs Class

Provides data for the RowContextMenuStripNeeded event.


Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public class DataGridViewRowContextMenuStripNeededEventArgs : EventArgs

The DataGridViewRowContextMenuStripNeededEventArgs type exposes the following members.

Public methodDataGridViewRowContextMenuStripNeededEventArgsInitializes a new instance of the DataGridViewRowContextMenuStripNeededEventArgs class.

Public propertyContextMenuStripGets or sets the shortcut menu for the row that raised the RowContextMenuStripNeeded event.
Public propertyRowIndexGets the index of the row that is requesting a shortcut menu.

Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

The RowContextMenuStripNeeded event occurs only when the DataGridView control DataSource property is set or its VirtualMode property is true. Handling the RowContextMenuStripNeeded event is useful when you want to display a shortcut menu determined by a row's current state or the values it contains.

When you handle the RowContextMenuStripNeeded event, the shortcut menu that you specify in the handler is shown whenever the user right-clicks a row unless the CellContextMenuStripNeeded event overrides the shortcut menu for the specific cell that was clicked.

The RowContextMenuStripNeeded event also occurs whenever the value of the DataGridViewRow.ContextMenuStrip property is retrieved, either programmatically or when the user right-clicks a row.

You can use the RowIndex property to determine the state of a row or the values it contains, and use this information to change or modify the ContextMenuStrip property. This property is initialized with the value of the row ContextMenuStrip property, which the event value overrides.

Handle the RowContextMenuStripNeeded event when working with large amounts of data to avoid the performance penalties of setting the row ContextMenuStrip value for multiple rows. For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

For more information about handling events, see Consuming Events.

The following code example handles the RowContextMenuStripNeeded event to provide a ContextMenuStrip based on the title of the employee. In this example, there are two shortcut menus, one for managers and one for all other employees.

#region Using directives

using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;


class Form1 : Form
    private DataGridView dataGridView1 = new DataGridView();
    private ContextMenuStrip employeeMenuStrip = new ContextMenuStrip();
    private ContextMenuStrip managerMenuStrip = new ContextMenuStrip();
    private ToolStripMenuItem toolStripMenuItem1 = new ToolStripMenuItem();
    private ToolStripMenuItem toolStripMenuItem2 = new ToolStripMenuItem();
    private ToolStripMenuItem toolStripMenuItem3 = new ToolStripMenuItem();

    private int contextMenuRowIndex;

    public Form1()
        // Initialize the form.
        // This code can be replaced with designer generated code.
        this.Size = new Size(700, 300);

        toolStripMenuItem1.Text = "View Employee Sales Report";
        toolStripMenuItem2.Text = "View Team Sales Report";
        toolStripMenuItem3.Text = "View Company Sales Team Ranking Report";

    protected override void OnLoad(EventArgs e)
        dataGridView1.DataBindingComplete +=
            new DataGridViewBindingCompleteEventHandler(
        dataGridView1.CellToolTipTextNeeded +=
            new DataGridViewCellToolTipTextNeededEventHandler(
        dataGridView1.RowContextMenuStripNeeded +=
            new DataGridViewRowContextMenuStripNeededEventHandler(
        toolStripMenuItem1.Click +=
            new EventHandler(toolStripMenuItem1_Click);
        toolStripMenuItem2.Click +=
            new EventHandler(toolStripMenuItem2_Click);
        toolStripMenuItem3.Click +=
            new EventHandler(toolStripMenuItem3_Click);





    // Establish the main entry point for the application.
    public static void Main()
        Application.Run(new Form1());

    // Replace this with your own code to populate the DataGridView.
    private void PopulateDataGridView()
        dataGridView1.Dock = DockStyle.Fill;
        dataGridView1.AllowUserToAddRows = false;
        dataGridView1.MultiSelect = false;
        dataGridView1.ReadOnly = true;
        dataGridView1.AllowUserToDeleteRows = false;

        string query;
        query = "SELECT e1.*, e2.FirstName + ' ' + e2.LastName AS Manager "
            + "FROM employees AS e1 LEFT JOIN employees AS e2 "
            + "ON e1.ReportsTo = e2.EmployeeID";

        // Connect to the database
        SqlConnection sqlConnection1 = new SqlConnection(
            "Integrated Security=SSPI;Persist Security Info=False;" +
            "Initial Catalog=Northwind;Data Source=localhost");
        SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(query,

        DataTable dataTable1 = new DataTable();
        dataTable1.Locale = System.Globalization.CultureInfo.InvariantCulture;

        dataGridView1.DataSource = dataTable1;

    private void dataGridView1_DataBindingComplete(object sender,
        DataGridViewBindingCompleteEventArgs e)
        // Hide some of the columns.
        dataGridView1.Columns["EmployeeID"].Visible = false;
        dataGridView1.Columns["Address"].Visible = false;
        dataGridView1.Columns["TitleOfCourtesy"].Visible = false;
        dataGridView1.Columns["BirthDate"].Visible = false;
        dataGridView1.Columns["HireDate"].Visible = false;
        dataGridView1.Columns["PostalCode"].Visible = false;
        dataGridView1.Columns["Photo"].Visible = false;
        dataGridView1.Columns["Notes"].Visible = false;
        dataGridView1.Columns["ReportsTo"].Visible = false;
        dataGridView1.Columns["PhotoPath"].Visible = false;

        // Disable sorting for the DataGridView.
        foreach (DataGridViewColumn i in
            i.SortMode = DataGridViewColumnSortMode.NotSortable;


    void dataGridView1_RowContextMenuStripNeeded(object sender,
        DataGridViewRowContextMenuStripNeededEventArgs e)
        DataGridViewRow dataGridViewRow1 = dataGridView1.Rows[e.RowIndex];

        toolStripMenuItem1.Enabled = true;

        // Show the appropriate ContextMenuStrip based on the employees title.
        if ((dataGridViewRow1.Cells["Title"].Value.ToString() ==
            "Sales Manager") ||
            (dataGridViewRow1.Cells["Title"].Value.ToString() ==
            "Vice President, Sales"))
            e.ContextMenuStrip = managerMenuStrip;
            e.ContextMenuStrip = employeeMenuStrip;

        contextMenuRowIndex = e.RowIndex;

    void dataGridView1_CellToolTipTextNeeded(object sender,
        DataGridViewCellToolTipTextNeededEventArgs e)
        string newLine = Environment.NewLine;
        if (e.RowIndex > -1)
            DataGridViewRow dataGridViewRow1 = dataGridView1.Rows[e.RowIndex];

            // Add the employee's ID to the ToolTipText.
            e.ToolTipText = String.Format("EmployeeID {0}:{1}",
                dataGridViewRow1.Cells["EmployeeID"].Value, newLine);

            // Add the employee's name to the ToolTipText.
            e.ToolTipText += String.Format("{0} {1} {2}{3}",

            // Add the employee's title to the ToolTipText.
            e.ToolTipText += String.Format("{0}{1}{2}",
                newLine, newLine);

            // Add the employee's contact information to the ToolTipText.
            e.ToolTipText += String.Format("{0}{1}{2}, ",
                dataGridViewRow1.Cells["Address"].Value.ToString(), newLine,
            if (!String.IsNullOrEmpty(
                e.ToolTipText += String.Format("{0}, ",
            e.ToolTipText += String.Format("{0}, {1}{2}{3} EXT:{4}{5}{6}",
                newLine, dataGridViewRow1.Cells["HomePhone"].Value.ToString(),
                newLine, newLine);

            // Add employee information to the ToolTipText.
            DateTime HireDate =
            e.ToolTipText +=
                String.Format("Employee since: {0}/{1}/{2}{3}Manager: {4}",
                HireDate.Month.ToString(), HireDate.Day.ToString(),
                HireDate.Year.ToString(), newLine,

    void toolStripMenuItem1_Click(object sender, EventArgs e)
        DataGridViewRow dataGridViewRow1 =

            "Sales Report for {0} {1}{2}{3}Reporting not implemented.",
            Environment.NewLine, Environment.NewLine));

    void toolStripMenuItem2_Click(object sender, EventArgs e)
        DataGridViewRow dataGridViewRow1 =
            "Sales Report for {0} {1}'s Team{2}{3}Reporting not implemented.",
            Environment.NewLine, Environment.NewLine));

    void toolStripMenuItem3_Click(object sender, EventArgs e)
        DataGridViewRow dataGridViewRow1 =
            "Company Sales Ranking Report:{0}{1}Reporting not implemented.",
            Environment.NewLine, Environment.NewLine));

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.