Articles in this section
Category / Section

How to add the ContextMenu to the GridTreeControl?

2 mins read

The GridTreeControl provides support to the ContextMenu interactive feature that is used to show the customizable menu for performing some functions in Grid. Here, Sorting, copy and paste operations are performed using ContextMenu.

In the GridTreeControl, you can show the context menu based on some criteria with the help of QueryContextMenuInfo event. To show the context menu by using the QueryContextMenuInfo event, you need to set the EnableContextMenu to true.

You can refer to the following code example to enable the ContextMenu in the grid.

 

C#

this.syncgrid.Model.EnableContextMenu = true;

 

By using the QueryContextMenuInfo event, you can get the details about cells like CellValue, CellType, column index, and row index. Based on those values, you can create your own ContextMenu.

In the following code example, ContextMenu for a grid is created based on the row index in the QueryContextMenuInfo event. Refer to the following code example to add the menu items in the ContextMenu based on the rowindex.

 

C#

this.syncgrid.Model.QueryContextMenuInfo += Model_QueryContextMenuInfo;
void Model_QueryContextMenuInfo(object sender, GridQueryContextMenuInfoEventArgs e)
{
    List<object> ContextMenuItems = new List<object>();
    MenuItem menuitem1 = new MenuItem { Background = Brushes.LightYellow, Header = "SortColumn", Command = ContextMenuCommand };
    MenuItem menuitem2 = new MenuItem { Background = Brushes.LightYellow, Header = "Copy", Command = ContextMenuCommand, CommandParameter = "Copy" };
    MenuItem menuitem3 = new MenuItem { Background = Brushes.LightYellow, Header = "Paste", Command = ContextMenuCommand, CommandParameter = "Paste" };
    //Columnindex resolved to sort the column.
    int columnIndex = this.syncgrid.InternalGrid.ResolveIndexToColumnIndex(e.Cell.ColumnIndex);
    string parameter = "Sort" + "_" + columnIndex.ToString();
    menuitem1.CommandParameter = parameter;
    if (e.Cell.RowIndex == 0)
    {
        //Menuitem1 added only to Header row to sort the column.
        ContextMenuItems.Add(menuitem1);
    }    
    else
    {
        //Menuitem2, menuitem3 added to copy and paste the node in a grid.
        ContextMenuItems.Add(menuitem2);
        ContextMenuItems.Add(menuitem3);
    }
    //Context menu added to the grid.
    e.Style.ContextMenuItems = ContextMenuItems;
}        

 

In the QueryContextMenuInfo event, when the row index is 0 (Header row), then the SortColumn menu item is added to the ContextMenu. For the other rows, Copy and Paste menu items are added. Refer to the following code example to perform the operations on clicking the menu items in the ContextMenu by using the Commands and CommandParameter of a menu item.

 

C#

private void ContextMenuInfo(object obj)
{
    var parameter = obj.ToString();
    if (parameter.Equals("Copy"))
        //Copies the node.
        syncgrid.Model.CutPaste.Copy();
    else if (parameter.Equals("Paste"))
        //Pastes the node.
        syncgrid.Model.CutPaste.Paste();
    else
    {
        string[] temp = parameter.Split('_');
        int columnIndex = -1;
        int.TryParse(temp[temp.Length - 1], out columnIndex);
        if (columnIndex > -1)
        {
            //columnName - Mapping name of the column that needs to be sorted.
            var columnName = this.syncgrid.Columns[columnIndex].MappingName;
            this.syncgrid.InternalGrid.SortTree(columnName, System.ComponentModel.ListSortDirection.Ascending);
        }
    }
}

On clicking the menu items, the following operations are performed.

  • Sort Column - Corresponding column is sorted.
  • Copy - Copy the selected node.
  • Paste - Copied node is pasted in the grid.

When you open the ContextMenu in the Header row, then the ContextMenu is displayed as follows.

Figure 1: Menu Item SortColumn added to ContextMenu

When you open the ContextMenu in other rows, then the ContextMenu is displayed as follows:

Figure 2: Copy, Paste Menu Items are added to Context Menu

Sample Link: AddContextMenuIn_GridTreeControl

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied