[Solved] How to subscribe to a mouse click event on a row in radgridview

Introduction

Subscribing to a row click event is not straight forward in WPF Telerik RadGridView. In this post we will see how we can achieve this. Subscribing to a mouse click event would have been straight forward if all the rows were available at the time of gridview loaded event. But as in most scenarios, not all rows are available at this time, so when you subscribe to a normal Mouse Click event, only a click on the Header row will trigger the events. As the GridView can be updated with values at a later stage, it is not easy to subscribe to those rows which are loaded at a later stage.

How to achieve this?

We can achieve this by subscribing to mouse events at some later point. The most appropriate one is when row is loaded, this is because whenever a new value is added to the gridview, this event will be fired, this way we can make sure that all rows will fire the mouse click event.

public ViewConstructor() {
/* Please note this is the constructor of your view*/
InitializeComponent();
RadGridView1.RowLoaded += this.RadGridViewRowLoaded;
}

private void RadGridViewRowLoaded(object sender, RowLoadedEventArgs e) {
   var row = e.Row as GridViewRow;

   if (row != null) {
        RadGridView1.AddHandler(GridViewRow.MouseLeftButtonDownEvent, new MouseButtonEventHandler(this.GridViewRowMouseLeftButtonDown), true);
      }
  }

private void GridViewRowMouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
    // Now you will start getting a call here when you click on a row.
}

Setting Up Kafka

Apache Kafka is a high-throughput, low-latency event processing system designed to handle millions of data feeds per second. It has the c...… Continue reading

Libish Varghese Jacob

Libish Varghese JacobI am currently working as a lead engineer in one of the leading wind turbine manufacturing firm. I have wide range of interests and getting my hands dirty in technology is one among them. I use this platform primarily as my knowledge base. I also use this platform to share my experience and experiments so that it might help someone who is walking the way I already did. The suggestions expressed here are the best to my knowledge at the time of writing and this may not necessarily be the best possible solution. I would pretty much appreciate if you could comment on it to bring into my notice on what could have been done better.