[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 a lead engineer at a prominent wind turbine manufacturing firm. My interests span a diverse range, and immersing myself in technology is one of them. This platform serves as my primary knowledge base, where I seek information and insights. Moreover, I utilize this platform to share my experiences and experiments, hoping they may benefit those following a similar path. It's important to note that the suggestions I express here are based on my best knowledge at the time of writing and may not necessarily represent the optimal solutions. I wholeheartedly welcome any comments you may have to improve or refine my ideas. Your feedback is greatly appreciated and valued.