New to Telerik UI for WinFormsStart a free 30-day trial

Create a custom context menu with copy cell/row functionality in RadGridView

Updated over 6 months ago

Environment

Product VersionProductAuthor
2019.2.618RadGridView for WinFormsNadya Karaivanova

Description

A common requirement is to copy the selected cell value or to copy the selected row value in RadGridView. This article shows how to achieve this funcionality in a custom context menu, so when right click in the RadGridView, the context menu opens, and you have copy cell/row functionality implemented. After selecting one of these, you can paste the content anywhere you need to (in Notepad or Excel, for example).

radgridview-copy-cell-row

Solution

To achieve this functionality, you should create a custom context menu in RadGridView and add two RadMenuItems. Once the menu object has been initialized and populated with menu items, it is ready to be attached to the RadGridView. To do that, you should subscribe to the ContextMenuOpening event and set the custom context menu to be displayed instead of the default one.

Setup the Context Menu

C#

 private RadContextMenu contextMenu;
 private void RadForm1_Load(object sender, EventArgs e)
 {

     contextMenu = new RadContextMenu();
     RadMenuItem copyCellItem = new RadMenuItem("Copy Cell");
     copyCellItem.ForeColor = Color.Red;
     copyCellItem.Click += CopyCellItem_Click;
     RadMenuItem copyRowItem = new RadMenuItem("Copy Row");
     copyRowItem.ForeColor = Color.Red;
     copyRowItem.Click += CopyRowItem_Click;
     contextMenu.Items.Add(copyCellItem);
     contextMenu.Items.Add(copyRowItem);
 }

 private void RadGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
 {
     e.ContextMenu = contextMenu.DropDown;
 }          

Thanks to the build-in Copy/Paste functionality which RadGridView supports, you can store the text in the Clipboard and then paste it in a different location. RadGridView copies the selected data considering the SelectionMode. If the SelectionMode property is set to FullRowSelect, the entire row will be copied. If it is set to CellSelect only the selected cell will be copied.

In the CopyCellItem_Click event handler you should first store the GridViewSelectionMode, set the SelectionMode to CellSelect, execute the Copy method, and then restore the SelectionMode.

In the CopyRowItem_Click event handler you should do the same, but set the SelectionMode to FullRowSelect. Here is the code snippet:

Copy cell/row functionality

C#

private void CopyCellItem_Click(object sender, EventArgs e)
{
    GridViewSelectionMode selecionMode = this.radGridView1.SelectionMode;
    this.radGridView1.SelectionMode = GridViewSelectionMode.CellSelect;
    this.radGridView1.CurrentRow.Cells[this.radGridView1.CurrentColumn.Name].IsSelected = true;
    this.radGridView1.Copy();
    this.radGridView1.SelectionMode = selecionMode;
    this.radGridView1.CurrentRow.IsSelected = true;
}

private void CopyRowItem_Click(object sender, EventArgs e)
{
    GridViewSelectionMode selecionMode = this.radGridView1.SelectionMode;
    this.radGridView1.SelectionMode = GridViewSelectionMode.FullRowSelect;
    this.radGridView1.CurrentRow.IsSelected = true;
    this.radGridView1.Copy();
    this.radGridView1.SelectionMode = selecionMode;
    this.radGridView1.CurrentRow.IsSelected = true; 
}
In this article
EnvironmentDescriptionSolution
Not finding the help you need?
Contact Support