This is a migrated thread and some comments may be shown as answers.

Winforms gridview Keeping default context menu for header row, while having custom context menu for data rows

4 Answers 27 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 22 Jul 2014, 05:44 PM
Hi all - I need to retain the default context menu available when right clicking in the header row.  I really don't want to have to re-create all the menus and what-not.  I know exactly where I need to do this decision (in the ContextMenuOpening call), but I can't figure out how to determine if the rightclick happened in the header row or not.

Can you suggest how I can replace the commented out code so this works?   Thanks!  Richard


void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
{
       //if ( mouse was clicked in the header row )
      // {
       // do nothing, we like the existing menu for that and don't want to reimplement it
      // } else

       if (radGridView1.SelectedRows.Count == 1)
       {
           e.ContextMenu = SingleRowSelectedcontextMenu.DropDown;
           cell = e.ContextMenuProvider as GridDataCellElement;
       }
       else if (radGridView1.SelectedRows.Count >= 1)
       {
            e.ContextMenu = MultiRowSelectedcontextMenu.DropDown;
            cell = e.ContextMenuProvider as GridDataCellElement;
        }
}

4 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 25 Jul 2014, 01:04 PM
Hello Richard,

Thank you for writing.

As you have already found out, the ContextMenuOpening event is the appropriate place to customize the drop down items. Here is a sample code snippet demonstrating how to determine the cell over which the context menu is opening and how to modify the items:
private void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
{
    Point pt = this.radGridView1.PointToClient(MousePosition);
    GridDataCellElement dataCell = this.radGridView1.ElementTree.GetElementAtPoint(pt) as GridDataCellElement;
    if (dataCell != null)
    {
        //if you right click over a data cell, customize the items in the menu
        e.ContextMenu.Items.Clear();
        e.ContextMenu.Items.Add(new RadMenuItem("Item"));
    }
}

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Richard
Top achievements
Rank 1
answered on 25 Jul 2014, 04:19 PM
Perfect, exactly what I needed!  Thanks for saving me a bunch of time!
Richard
0
Jeremy
Top achievements
Rank 1
answered on 01 Aug 2014, 07:32 PM
Hi,

How can I accurately identify the difference between right clicking on a cell/row that contains the filter criteria vs. the cell/row that contains the column header vs. the cell/row that contains the actual data?
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 06 Aug 2014, 01:58 PM
Hello guys,

Thank you for writing.

@Richard,
I am glad that the suggested solution suits your scenario. If you have any additional questions, please let me know.

@Jeremy
Here is a sample code snippet, demonstrating how to distinguish for which cell element the context menu is opening when pressing right mouse button. Similar approach can be used in the MouseDown event:
private void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
{
    Point pt = this.radGridView1.PointToClient(MousePosition);
    GridDataCellElement dataCell = this.radGridView1.ElementTree.GetElementAtPoint(pt) as GridDataCellElement;
    GridFilterCellElement filterCell = this.radGridView1.ElementTree.GetElementAtPoint(pt) as GridFilterCellElement;
    GridHeaderCellElement headerCell = this.radGridView1.ElementTree.GetElementAtPoint(pt) as GridHeaderCellElement;
    if (dataCell != null)
    {
        //if you right click over a data cell
    }
    else if (filterCell != null)
    {
        //if you right click over a filter cell
    }
    else if (headerCell!=null)
    {
        //if you right click over a header cell
    }
}

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
GridView
Asked by
Richard
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Richard
Top achievements
Rank 1
Jeremy
Top achievements
Rank 1
Share this question
or