Copy Datagridview cell value using ContextMenu


                    The following article demonstrates copying of cell values inside a winforms Datagridview using a contextmenu.


Datagridview cell value copy program

Declare a new contextmenu and add a Menuitem

ContextMenu contextMenu = new ContextMenu();
            contextMenu.MenuItems.Add(new MenuItem("Copy", CopyClick));

CopyClick is the name of the method that is called when user clicks on "Copy".

 Cache the activecell of the grid in the ActiveCell variable, as it contains the value to be copied.
      DataGridViewCell ActiveCell = null;


The CopyClick method codes is as below

private void CopyClick(object sender, EventArgs e)
        {
            if (ActiveCell != null && ActiveCell.Value != null)
                Clipboard.SetText(ActiveCell.Value.ToString());
        }


The logic to show the contextmenu on right click of mouse is as below

  private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                System.Windows.Forms.DataGridView.HitTestInfo hittestinfo =
                                                                          dataGridView1.HitTest(e.X, e.Y);

                if (hittestinfo != null && hittestinfo.Type == DataGridViewHitTestType.Cell)
                {
                    ActiveCell = dataGridView1[hittestinfo.ColumnIndex, hittestinfo.RowIndex];
                    ActiveCell.Selected = true;
                    contextMenu.Show(dataGridView1, new Point(e.X, e.Y));
                }

            }
        }


HitTestInfo class contains the coordiantes of the mouse click and row and column indexes of the mouse click.

Download Source Code

Zooming in Winforms Webbrowser Control in .NET
Filtering Datatable with Select Conditions and Sorting in C#.NET
Hiding a property from displaying in Datagridview in C#.NET
Logic to detect if Application is started from Citrix/Terminal Server/PC in C#
Winforms Application.DoEvents Method
Modify XML file with tagname in c#.net
Read Machine IP Address in C#
Check if IIS is running using C#
Implementing IEnumerable in C#
Built in Delegates in .NET Framework
Changing Winform webbrowser control IE version in C#.NET
Set the dropdown width of any combobox based on the data
Difference between Const and ReadOnly
Example for Property Change Notification in C#.NET
Example Program using Delegates and Events in C#
AutoComplete in .NET Winforms TextBox
How to read data from XML String and insert in to table in SQL Server
WebBrowser.navigate Data Submission by Get or Post Methods
Datagridview Paging
Inserting data in Datatable in C#
Cookie in .NET
Using Control's Child Index and Dock property for achieving desired layout in a form.
Accessing section group inside configuration of web.config
Create File in .NET C#
Change button shape in .net
Dispose vs Finalize in .net




Datagridview cell value copy c# contextmenu , datagrid cell text copy, datagridview column value copy