Articles in this section
Category / Section

How to use your own context menu along with the FieldChooser option?

1 min read

Description:

By default, the FieldChooser context menu is displayed when you right-click on the ColumnHeadercell of the Grid but it is not displayed when you right-click on the Grid cells.

C#

//declared outside of the form constructor for public access
FieldChooser fieldchooser;
//In Form constructor
fieldchooser = new FieldChooser(this.gridGroupingControl1);

 

VB

'declared outside of the form constructor for public access
Dim fieldchooser As FieldChooser
' In Form constructor
fieldchooser = New FieldChooser(Me.gridGroupingControl1)

 

When you try to use your own context menu along with the FieldChooser context menu item, it overrides the FieldChooser context menu.

Solution:

The context menu that is displaying when you right-click the ColumnHeadercell can be restricted by the TableControlMouseDown event. By using this event, you can customize ContextMenuStrip to display only in the cells other than the header cells.

C#

// user defined context menu
contextMenuStrip = new ContextMenuStrip();            
contextMenuStrip.Items.Add("Copy");
contextMenuStrip.Items.Add("Paste");
contextMenuStrip.ItemClicked += new ToolStripItemClickedEventHandler(contextMenuStrip_ItemClicked);
this.gridGroupingControl1.TableControlMouseDown += gridGroupingControl1_TableControlMouseDown;
void contextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
    if (e.ClickedItem.Text == "Copy")
    {
        int count = this.gridGroupingControl1.Table.SelectedRecords.Count;
            MessageBoxAdv.Show(count.ToString());
    }
}
void gridGroupingControl1_TableControlMouseDown(object sender, GridTableControlMouseEventArgs e)
{
int row = 0, col = 0; 
this.gridGroupingControl1.TableControl.PointToRowCol(e.Inner.Location, out row, out col);
GridTableCellStyleInfo style = this.gridGroupingControl1.TableControl.GetTableViewStyleInfo(row,col);
  //To check whether it is columnheadercell
  if (style != null && style.TableCellIdentity.TableCellType == GridTableCellType.ColumnHeaderCell  && e.Inner.Button == System.Windows.Forms.MouseButtons.Right)
  { 
    gridGroupingControl1.ContextMenuStrip = fieldchooser.ContextMenu; 
  }
  else//If it is not column header cell
  {
    gridGroupingControl1.ContextMenuStrip =contextMenuStrip;
  }
}

 

VB

' user defined context menu
contextMenuStrip = New ContextMenuStrip()
contextMenuStrip.Items.Add("Copy")
contextMenuStrip.Items.Add("Paste")
AddHandler contextMenuStrip.ItemClicked, AddressOf contextMenuStrip_ItemClicked
AddHandler Me.gridGroupingControl1.TableControlMouseDown, AddressOf gridGroupingControl1_TableControlMouseDown
void contextMenuStrip_ItemClicked(Object sender, ToolStripItemClickedEventArgs e)
  If e.ClickedItem.Text = "Copy" Then
    Dim count As Integer = Me.gridGroupingControl1.Table.SelectedRecords.Count
      MessageBoxAdv.Show(count.ToString())
  End If
void gridGroupingControl1_TableControlMouseDown(Object sender, GridTableControlMouseEventArgs e)
Dim row As Integer = 0, col As Integer = 0
Me.gridGroupingControl1.TableControl.PointToRowCol(e.Inner.Location, row, col)
Dim style As GridTableCellStyleInfo = Me.gridGroupingControl1.TableControl.GetTableViewStyleInfo(row,col)
  'To check whether it is columnheadercell
  If style IsNot Nothing AndAlso style.TableCellIdentity.TableCellType = GridTableCellType.ColumnHeaderCell AndAlso e.Inner.Button = System.Windows.Forms.MouseButtons.Right Then
                gridGroupingControl1.ContextMenuStrip = fieldchooser.ContextMenu
  Else 'If it is not column header cell
                gridGroupingControl1.ContextMenuStrip =contextMenuStrip
  End If

 

The following screenshot displays the context menu.

C:\Users\venkatesh s\Desktop\chooser.png

 

Sample links:

C#: Contextmenu

VB: Contextmenu

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