Articles in this section
Category / Section

How to show different context menu for each column in WPF DataGrid?

2 mins read

WPF DataGrid (SfDataGrid) allows you to set different ContextMenu for each column using the SfDataGrid.GridContextMenuOpening event.

Refer to the following code example to define different ContextMenu for each column based on the column index in SfDataGrid.GridContextMenuOpening event. Here, the first three columns have different ContextMenu and the rest of columns have different ContextMenu,

 

this.AssociatedObject.GridContextMenuOpening += AssociatedObject_GridContextMenuOpening;
 
private void AssociatedObject_GridContextMenuOpening(object sender, GridContextMenuEventArgs e)
{
            e.ContextMenu.Items.Clear();
            var columnIndex = this.AssociatedObject.ResolveToGridVisibleColumnIndex(e.RowColumnIndex.ColumnIndex);
            if (columnIndex < 0 && columnIndex >= this.AssociatedObject.Columns.Count)
                return;
            var column = this.AssociatedObject.Columns[columnIndex];
            if (column == null)
                return;
 
            List<object> commandParameter = new List<object>();
            commandParameter.Add(this.AssociatedObject);
            commandParameter.Add(column);
            CommandBinding binding = new CommandBinding();
            this.AssociatedObject.CommandBindings.Add(binding);
 
            if (columnIndex < 3)
            {
                e.ContextMenu.Items.Add(new MenuItem()
                {
                    Header = "AddSort",
                    Command = ContextMenuCommands.AddSort,
                    CommandParameter = commandParameter
                });
                e.ContextMenu.Items.Add(new MenuItem()
                {
                    Header = "AddGroup",
                    Command = ContextMenuCommands.AddGroup,
                    CommandParameter = commandParameter
                });
                e.ContextMenu.Items.Add(new MenuItem()
                {
                    Header = "ClearSort",
                    Command = ContextMenuCommands.ClearSort,
                    CommandParameter = commandParameter
                });
                e.ContextMenu.Items.Add(new MenuItem()
                {
                    Header = "ClearGroup",
                    Command = ContextMenuCommands.ClearGroup,
                    CommandParameter = commandParameter
                });
            }
            else 
            {
                e.ContextMenu.Items.Add(new MenuItem()
                {
                    Header = "Copy",
                    Command = ContextMenuCommands.Copy,
                    CommandParameter = commandParameter
                });
                e.ContextMenu.Items.Add(new MenuItem()
                {
                    Header = "Paste",
                    Command = ContextMenuCommands.Paste,
                    CommandParameter = commandParameter
                });
       }
}

 

Context menu for column in WPF DataGrid

View WPF DataGrid Context Menu Demo in GitHub

 

Conclusion

I hope you enjoyed learning about how to show different context menu for each column in WPF DataGrid.

You can refer to our WPF DataGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our WPF DataGrid documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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