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

ContextMenu in GridView

3 Answers 340 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Baldwin
Top achievements
Rank 1
Baldwin asked on 25 Nov 2010, 01:52 PM
Hello everybody.

I'm having some problems with applying a custom context menu to a grid view.
While it works without problems for must cases, I encounter problems in these scenarios:

  1. My grid view contains a check box column. I cannot get any kind of context menu opening on that column (neither default nor custom).
  2. I would like to have the custom menu open as well, when someone right-clicks the column header of a specific column.

Thanks for any help,
Baldwin

3 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 25 Nov 2010, 05:17 PM

Hello,

I am unsure if it is possible to open a context menu on a GridViewCheckBox column. The RadgridView ContextMenuOpening event does not fire on this. I think you would need to create a custom cell element that contains a checkbox. this forum post contains lots of information for creating custom columns

For your second issue, you can create a custom context menu for the header based on a specific header cell in the following way.

1: declare a menu

Private m_DataContextMenu As New RadDropDownMenu()

2: in form_load (for example)

Dim myHeaderMeniItem As New RadMenuItem("Please click this header cell")
AddHandler myHeaderMeniItem.Click, AddressOf MyHeaderMenuClick
m_DataContextMenu.Items.Add(myHeaderMeniItem)

3:  Handle the click event
Private Sub MyHeaderMenuClick(ByVal sender As System.Object, ByVal e As EventArgs)
    MessageBox.Show("You clicked the context menu")
End Sub

4: Declare a Point so we can get an element location
Private m_Point As System.Drawing.Point

5: handle the mouse move event to set the point
Private Sub RadGridView1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RadGridView1.MouseMove
    m_Point = e.Location
End Sub

6:  Handle the ContextMenuOpening event of the RadGridView
Private Sub RadGridView1_ContextMenuOpening(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.ContextMenuOpeningEventArgs) Handles RadGridView1.ContextMenuOpening
    If TypeOf e.ContextMenuProvider Is GridHeaderCellElement Then
        Dim element As RadElement = Me.RadGridView1.ElementTree.GetElementAtPoint(m_Point)
        If TypeOf element Is GridHeaderCellElement Then
            If DirectCast(element, GridHeaderCellElement).Text = "MyColumnHeaderText" Then
                e.ContextMenu = m_DataContextMenu
            End If
        End If
    End If
End Sub


hope this helps but let me know if you have any other questions
richard
0
Baldwin
Top achievements
Rank 1
answered on 26 Nov 2010, 10:30 AM
Thanks for the answer.

Opening the context menu on the header cell was no problem with your guide, though I implemented a bit different:

 

private void grid1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
{
    if (e.ContextMenuProvider is GridHeaderCellElement)
    {
        GridHeaderCellElement headerCell = e.ContextMenuProvider as GridHeaderCellElement;
        if ((headerCell as GridHeaderCellElement).ColumnInfo.Name == "ColumnName")
            e.ContextMenu = contextMenu1.DropDown;
    }
}

As For the Context Menu on CheckBox columns: That is not that important, I had hoped that there is an easy solution.

So thanks.

0
Richard Slade
Top achievements
Rank 2
answered on 26 Nov 2010, 10:34 AM
Hi Baldwin,

I'm gald that helped you. Your final solution is neater, I guess I was getting tired yesterday.
Best regards,
Richard
Tags
GridView
Asked by
Baldwin
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Baldwin
Top achievements
Rank 1
Share this question
or