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

Empty grid and contextmenu.

3 Answers 376 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jose Mejia
Top achievements
Rank 1
Jose Mejia asked on 18 Aug 2010, 02:06 PM
Hello everybody!

I have empty grid (Rows.Count == 0) and I need that  it's context menu openning on this grid.
Is it possible to force context menu openning handling, for example, right mouse click?

3 Answers, 1 is accepted

Sort by
0
Accepted
Alexander
Telerik team
answered on 23 Aug 2010, 02:01 PM
Hello Jose Mejia,

Thank you for your question.

You can create a custom context menu by using this code snippet:

private RadDropDownMenu menu;
 
private void CreateCustomMenu()
{
    menu = new RadDropDownMenu();
    RadMenuItem menuItem = new RadMenuItem("Item1");
    menuItem.Click += new EventHandler(menuItem_Click);
    menu.Items.Add(menuItem);
}

Then you can use the MouseDown event of the RadGridView control to show the context menu:

private void radGridView1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button != MouseButtons.Right)
    {
        return;
    }
 
    menu.Show(this.radGridView1, e.X, e.Y);
}
 
private void menuItem_Click(object sender, EventArgs e)
{
}

I hope it helps in your scenario.

Best regards,
Alexander
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Jose Mejia
Top achievements
Rank 1
answered on 25 Aug 2010, 07:55 AM
Hello.
Thanks for reply.
One more question:
I have following code for handling menu openning:
private void _pointGridView_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
        {
             
             
            if (_pointGridView.CurrentRow is GridViewDataRowInfo)
            {
                if (_pointContextMenu == null || _pointContextMenu.IsDisposed)
                {
                    _pointContextMenu = GetPointMenu();
                }
 
                e.ContextMenu = _pointContextMenu;
                e.Cancel = false;
                return;
            }
 
            for (int i = 0; i < e.ContextMenu.Items.Count; i++)
            {
                if (!PointFilterMenuItems.Contains(e.ContextMenu.Items[i].Text))
                {
                     
                    e.ContextMenu.Items[i].Visibility = ElementVisibility.Collapsed;
                }
            }
        }

It works great but when I click on a column header it shows context menu (because expr(_pointntGridView.CurrentRow is GridViewDataRowInfo) is true). Is there any possibility to prevent this? For example, when I click on a column header nothing happens.
0
Accepted
Alexander
Telerik team
answered on 27 Aug 2010, 04:56 PM
Hello Jose Mejia,

Thank you for writing me back.

Please consider the following code snippet. It demonstrates how to check whether the clicked cell is from the header row:

private void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
{
    if (e.ContextMenuProvider is GridHeaderCellElement)
    {
        e.Cancel = true;
    }
}

I hope it helps. Please do not hesitate to write back, if you have further questions.

Kind regards,
Alexander
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Jose Mejia
Top achievements
Rank 1
Answers by
Alexander
Telerik team
Jose Mejia
Top achievements
Rank 1
Share this question
or