FlexGrid for WinForms | ComponentOne
Grid / Basic Operations
In This Topic
    Basic Operations
    In This Topic

    This topic discusses about various operations that require handling at the grid level.

    Display Context Menu

    Context menus can be helpful to user as they provide shortcuts for actions that are frequently used. In FlexGrid, there can be two scenarios to display a context menu.

    Display Context Menu in Non-edit Mode

    To display context menu when grid is in non-edit mode, you need to create an instance of ContextMenuStrip control, add the menu items and assign the instance to ContextMenuStrip property of the Control class.

    Display Context Menu in Non-edit Mode

    Refer to the code below to see how to display a context menu in WinForms FlexGrid in non-edit mode.

    // Create an instance of ContextMenuStrip control
    ContextMenuStrip cm = new ContextMenuStrip();
    
    // Add menu items to the context menu
    cm.Items.Add("Add Above");
    cm.Items.Add("Add Below");
    cm.Items.Add("Add Left");
    cm.Items.Add("Add Right");
    
    // Assign the instance to ContextMenuStrip property 
    c1FlexGrid1.ContextMenuStrip = cm;                       
    
    ' Create an instance of ContextMenuStrip control
    Dim cm As ContextMenuStrip = New ContextMenuStrip()
    
    ' Add menu items to the context menu
    cm.Items.Add("Add Above")
    cm.Items.Add("Add Below")
    cm.Items.Add("Add Left")
    cm.Items.Add("Add Right")
    
    ' Assign the instance to ContextMenuStrip property 
    c1FlexGrid1.ContextMenuStrip = cm 
    

    Display Context Menu in Edit Mode

    To display context menu in edit mode, you need to display the context menu on editor by using StartEdit event of the C1FlexGrid class. In the StartEdit event, instantiate the editor and the ContextMenuStrip, add menu items and then assign it to ContextMenuStrip property of the editor.

    Display Context Menu in Edit Mode

    Use the code below to display context menu in WinForms FlexGrid in edit mode.

    private void C1FlexGrid1_StartEdit(object sender, C1.Win.C1FlexGrid.RowColEventArgs e)
    {
         TextBox tb = (TextBox)c1FlexGrid1.Editor;
    
        // Create context menu
        ContextMenuStrip cm2 = new ContextMenuStrip();
        cm2.Items.Add("Cut");
        cm2.Items.Add("Copy");
        cm2.Items.Add("Paste");
       
        // Set context menu 
        tb.ContextMenuStrip = cm2;
    }                      
    
    Private Sub C1FlexGrid1_StartEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs)
        Dim tb As TextBox = CType(c1FlexGrid1.Editor, TextBox)
    
        ' Create context menu
        Dim cm2 As ContextMenuStrip = New ContextMenuStrip()
        cm2.Items.Add("Cut")
        cm2.Items.Add("Copy")
        cm2.Items.Add("Paste")
    
        ' Set context menu 
        tb.ContextMenuStrip = cm2
    End Sub        
    
    See Also