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

Single Click event on GridView row

10 Answers 1266 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 29 May 2013, 02:43 AM
I have done quite a bit of searching and I can't seem to find a solution for this. We have a GridView that is associated with a map. The application is an asset tracking application where users can see different assets in the grid (with some associated data). Here is what is required:

- Click once on a row and the map is zoomed to that single point (i have this working, using the SelectionChanged event)
- Click on the same row again and reset back to the full map.

The second bullet is what I am having a hard time with. I can't seem to just capture single clicks. Any ideas?

10 Answers, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 30 May 2013, 11:05 AM
Hi,

The approach would be to add a handler directly to RadGridViewRow. Please check the following code snippet for a reference:

this.AddHandler(GridViewRow.MouseLeftButtonDownEvent,
             new MouseButtonEventHandler(OnMouseLeftDown), true);

public void OnMouseLeftDown(object sender, MouseButtonEventArgs e)        
 {         }


I hope this helps.


Regards,
Yoan
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Michael
Top achievements
Rank 1
answered on 30 May 2013, 07:55 PM
Yes that helps... but the requirement has changed a bit. Let me see if you can help me with this one:

I need to be able to manage single and double clicks on a row. Currently I am using the MouseDown for a single click (works fine) and the MouseDoubleClick for double clicks. Problem is the single event always overrides the double click. I can I manage the two of them?
0
Yoan
Telerik team
answered on 04 Jun 2013, 04:48 PM
Hi Michael,


In this case, I can suggest you to handle the PreviewMouseDown event in combination with a Timer. I have attached a sample project which shows you the approach. Please take a look at it and let me know if you need any further assistance.



Regards,
Yoan
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
parmod
Top achievements
Rank 1
answered on 17 Sep 2019, 03:57 PM

Hi Micheal,

 

With the above event handling. Single mouse click is also firing on the empty area of the RADGRIDVIEW. It should be fire on the data grid rows.

 

Thanks,

Parmod

0
Yoan
Telerik team
answered on 19 Sep 2019, 12:34 PM

Hi Parmod,

Indeed, you are absolutely right. You can register a class handler like so: 

            EventManager.RegisterClassHandler(typeof(GridViewRow), GridViewRow.MouseLeftButtonDownEvent, new RoutedEventHandler(OnRowMouseDown),true);

and the handler will be invoked only when clicking on the GridViewRow element.

Regards,
Yoan
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
christer
Top achievements
Rank 1
answered on 09 Aug 2020, 07:31 AM
How can this be done with MVVM pattern?
0
Yoan
Telerik team
answered on 12 Aug 2020, 03:00 PM

Hi Christer,

Can you share what exactly is the final goal that you want to achieve? We can think of another solution that can be more suitable for the MVVM pattern.

Regards,
Yoan
Progress Telerik

0
christer
Top achievements
Rank 1
answered on 12 Aug 2020, 03:32 PM

Hi Joan,

I would like to do different action if I just singleclick on the row or if I doubleclick on the row. I have tried to have an EventToCommandBehavior.EventBindings for each event (MouseDown and MouseDoubleClick). But then if I double click the row both events are fired which I don't want. Singleclick should move the selected item in the grid to another grid. And doubleclick should open an editor window for the item in the gridrow. The program will be used on a touchscreen so I want to avoid change to "right click" which was one idea.

0
Yoan
Telerik team
answered on 13 Aug 2020, 06:22 AM

Hi Christer,

Indeed, using the EventToCommand will not work for you in this scenario because you need to separate the events (The MouseDown event will be always firing). So, I can suggest sticking with the approach that I provided in this thread - using a Timer.

If you want to have your code-behind clean, the way to go is with an Attached behavior. Here you can find information about it. You will write the attached behavior only once. This attached behavior will attach to the event and you will be able to apply the functionality in it. Here is a very nice article about event handling with attached behaviors. You have the code in one place only. Then you can enable this attached behavior on a RadGridView. All you will need to do is something like this:

<RadGridView MyBehavior.IsEnabled="True"/> and then let the attached behavior kick in and do its job.

Regards,
Yoan
Progress Telerik

0
christer
Top achievements
Rank 1
answered on 13 Aug 2020, 07:32 AM
Thank you Yoan. This worked great!
Tags
GridView
Asked by
Michael
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Michael
Top achievements
Rank 1
parmod
Top achievements
Rank 1
christer
Top achievements
Rank 1
Share this question
or