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

Contextmenu not to show on the mouse header click

3 Answers 69 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Beatrice
Top achievements
Rank 1
Beatrice asked on 27 Apr 2016, 08:45 PM

Hi,

 

I have a popup contextmenu that needs to show on the mouse right click on the data cell, BUT SHOULD NOT SHOW when the user clicks or right clicks on the grid header.

Here is my code. How to capture the Grid header click by the mouse?

Private Sub grdInstPymt_MouseDown(sender As Object, e As MouseEventArgs) Handles grdInstPymt.MouseDown
Try
     If e.Button = System.Windows.Forms.MouseButtons.Right Then
         Me.ContextMenuStrip = PopUpMenu
         PopUpMenu.Show()
     End If
Catch ex As Exception
End Try
End Sub

3 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 28 Apr 2016, 08:07 AM
Hello Beatrice,

Thank you for writing.

You can use the GetElementAtPoint method to check what is clicked:
private void RadGridView1_MouseDown(object sender, MouseEventArgs e)
{
    var clickedElemnt = radGridView1.ElementTree.GetElementAtPoint(e.Location);
    if (clickedElemnt is GridDataCellElement)
    {
        //show your menu here
    }
}

Let me know if you have additional questions.

Regards,
Dimitar
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Beatrice
Top achievements
Rank 1
answered on 28 Apr 2016, 05:27 PM

What type would I declare the variable clickedElemnt?

Dim clickedElemnt as ???

clickedElemnt = radgrid1.ElementTree.GetElementAtPoint(e.Location)
If (clickedElemnt Is GridDataCellElement) Then
     Me.ContextMenuStrip = PopUpMenu
     PopUpMenu.Show()
End If

0
Accepted
Dimitar
Telerik team
answered on 29 Apr 2016, 05:13 AM
Hello Beatrice,

Thank you for writing back.

Yes, you do not need to declare a specific type in this case. The code in Visual Basic will look like this:
Private Sub RadGridView1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
    Dim clickedElemnt = radGridView1.ElementTree.GetElementAtPoint(e.Location)
    If TypeOf clickedElemnt Is GridDataCellElement Then
        'show your menu here
    End If
End Sub

Another approach would be to cast the object first:
Private Sub RadGridView1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
    Dim clickedElemnt = TryCast(radGridView1.ElementTree.GetElementAtPoint(e.Location), GridDataCellElement)
    If clickedElemnt IsNot Nothing Then
        'show your menu here
    End If
End Sub

Let me know if I can assist you further.

Regards,
Dimitar
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
GridView
Asked by
Beatrice
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Beatrice
Top achievements
Rank 1
Share this question
or