.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Windows Application >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Add context menu in datagridview in Windows Application

Posted By:Amit Mehra       Posted Date: May 14, 2013    Points: 200    Category: Windows Application    URL: http://www.dotnetspark.com  

In this tutorial we shall learn how add add context menu or RMB options in datagridview in windows application using C# Code sample.
 

This article we shall learn how to add context menu in DataGridView in windows application.

1st create a class employee so that we can add some dummy data using this class to DataGridView.

Note: Context menu will not work if DataGridView do not have any data.

Below is the image from RMB menu from DataGridView

 

Lets create class

 

// Craete an Employee class and add some properties to it
public class Employee
{
    int _ID = int.MinValue;

    public int ID
    {
        get { return _ID; }
        set { _ID = value; }
    }
    string _FirstName = string.Empty;

    public string FirstName
    {
        get { return _FirstName; }
        set { _FirstName = value; }
    }
    string _LastName = string.Empty;

    public string LastName
    {
        get { return _LastName; }
        set { _LastName = value; }
    }

    public Employee( int id , string firstName , string lastName )
    {
        this._ID = id;
        this._FirstName = firstName;
        this._LastName = lastName;
    }
}

On Button click event I have created contextMenu which will be bind to DataGridView also created some data using Employee

class. In this article our DataGridView name is myDataGridView.

 

private void button1_Click( object sender , EventArgs e )
{
    ContextMenuStrip contextMenu = new ContextMenuStrip( );

    // Add items to the contextMenu string object
    contextMenu.Items.Add( "Export Data" );
    contextMenu.Items.Add( "Add Data" );
    contextMenu.Items.Add( "Edit Data" );
    contextMenu.Items.Add( "Delete Data" );


    contextMenu.Opening += new CancelEventHandler( contextMenu_Opening );
    contextMenu.ItemClicked += new ToolStripItemClickedEventHandler( contexMenu_ItemClicked );

    //Assign contextmenuStrip to DataGridView's ContextMenuStrip property
    myDataGridView.ContextMenuStrip = contextMenu;

    try
    {
        // Create collection object
        List<Employee> empCollection = new List<Employee>( );

        //Add some data to the collection objects
        empCollection.Add( new Employee( 1 , "Sam" , "Walter" ) );
        empCollection.Add( new Employee( 2 , "Ram" , "Khanna" ) );
        empCollection.Add( new Employee( 3 , "Bob" , "Smith" ) );
        empCollection.Add( new Employee( 4 , "Jobin" , "Joe" ) );

        //Bind to a dataGridview
        myDataGridView.DataSource = empCollection;
    }
    catch( Exception ex )
    {
        MessageBox.Show( ex.Message );
    }
}


Below is the extension method to show RMB Menu once user click right mouse button on DataGridView


 

/// <summary>
/// Extension method to show context menu
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void contextMenu_Opening( object sender , CancelEventArgs e )
{
    ContextMenuStrip menu = sender as ContextMenuStrip;

    if( menu != null )
    {
        menu.Show( );
    }
}


In below event method we can write some code which we want to perform on click of a perticular item from RMB options

 

/// <summary>
/// Do something when item is clicked from context menu
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void contexMenu_ItemClicked( object sender , ToolStripItemClickedEventArgs e )
{
    try
    {
        ToolStripItem item = e.ClickedItem;

        if( e.ClickedItem.Text.ToLower( ) == "export data" )
        {
            //Do some operation when export data is clicked from RMB options
        }
    }
    catch( Exception ex )
    {
        MessageBox.Show( ex.Message );
    }
}

You can also download source code of the above article.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend