Home Forums WinForms controls Xceed Grid for WinForms Context menu strip

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #17055 |

    Hey all,

     I am trying to add ContextMenuStrip to Grid.

    Problem I am facing is, when I assign to GridControl It will be available to all Column Headers and rest of it too.

    All I need is I need to have contextMenu to Only Selected CELL not to the RowSelector or Row or ColumnManager or Column.

    What is the easiest way to do so??

    NR

    Imported from legacy forums. Posted by NR (had 3486 views)

    User (Old forums)
    Member
    Post count: 23064

    You can use the the MouseDown event at the cell level and verify if the cell is current, and if so let the ContextMenu open.  In all other circumstances, do not let the ContextMenu open, using the Opening event on the ContextMenuStrip.

    e.g.:

        private void Form1_Load( object sender, EventArgs e )
        {
          gridControl1.ContextMenuStrip = contextMenuStrip1;
          contextMenuStrip1.Opening += new CancelEventHandler( contextMenuStrip1_Opening );

          foreach( Cell cell in dataRowTemplate1.Cells )
          {
            cell.MouseDown += new MouseEventHandler(cell_MouseDown);
          }
        }

        private bool onCurrentCell = false;

        void cell_MouseDown( object sender, MouseEventArgs e )
        {
          Cell cell = sender as Cell;
          if( cell != null && cell.IsCurrent )
          {
            onCurrentCell = true;
          }
          else
          {
            onCurrentCell = false;
          }
        }

        void contextMenuStrip1_Opening( object sender, CancelEventArgs e )
        {
          if( !onCurrentCell )
          {
            onCurrentCell = false;
            e.Cancel = true;
          }
        }
     

     

    Imported from legacy forums. Posted by André (had 2173 views)

    User (Old forums)
    Member
    Post count: 23064

    Thank you very much it works fantastic.

    Imported from legacy forums. Posted by NR (had 2044 views)

    User (Old forums)
    Member
    Post count: 23064

    Hey there,

    I have a few issues with that.

    What I want to achieve is I dont want to Enable or open ContextMenu on Column Headers and only on Cell of GridRows,

    Another thing is my grid is just a readonly grid and dont want CellNavigation so in that case to check the IsCurrent I need to Enable that.

    The only reason I dont want CellNavigation Turned off is I could not fnd a way to remove that Rectangle in readonly grid for a cell when it is read only and non-editable but still shows the current Cell. If I try to change the Color properties of it it stays there when I move to another row or I have to go and change it again.

     So need something simple which lets me do Only on Cells but without cell navigation at all.

    Sorry to post again but just a change if requirement..

    NR

    Imported from legacy forums. Posted by NR (had 572 views)

    User (Old forums)
    Member
    Post count: 23064

    You can set the ShowFocusRectangle property on the GridControl to false, which will permit you to enable CellNavigation.

    Then you can add an event handler for the MouseDown of the ColumnManagerCell’s, and add a verification on the type of cell that is current in the handler.  Note that if you have other types of rows in the grid that can have the current cell set on it (e.g. InsertionRow), you will need to subscribe to the same MouseDown event on it.

    e.g.:

        private void Form1_Load( object sender, EventArgs e )
        {
          gridControl1.ReadOnly = true;
          gridControl1.ShowFocusRectangle = false;
          gridControl1.ContextMenuStrip = contextMenuStrip1;
          contextMenuStrip1.Opening += new CancelEventHandler( contextMenuStrip1_Opening );

          foreach( Cell cell in dataRowTemplate1.Cells )
          {
            cell.MouseDown += new MouseEventHandler(cell_MouseDown);
          }

          foreach( ColumnManagerCell cell in columnManagerRow1.Cells )
          {
            cell.MouseDown += new MouseEventHandler(cell_MouseDown);
          }

        }

        private bool onCurrentCell = false;

        void cell_MouseDown( object sender, MouseEventArgs e )
        {
          Cell cell = sender as Cell;
          if( cell != null && cell.IsCurrent && cell.GetType() == typeof( DataCell ) )
          {
            onCurrentCell = true;
          }
          else
          {
            onCurrentCell = false;
          }
        }

        void contextMenuStrip1_Opening( object sender, CancelEventArgs e )
        {
          if( !onCurrentCell )
          {
            e.Cancel = true;
          }
            onCurrentCell = false;
        }

    Imported from legacy forums. Posted by André (had 2219 views)

    User (Old forums)
    Member
    Post count: 23064

    thanx mate its working better now.

     

    thanx for your help 

    Imported from legacy forums. Posted by NR (had 3442 views)

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.