WPF DataGrid Row MouseDoubleClick Example

  • by
WPF DataGrid MouseDoubleClick 01

WPF DataGrid MouseDoubleClick Example:

Let’s see How we can create WPF DataGrid MouseDoubleClick method. WPF DataGrid with Mouse double click methods can be used for capturing the clicked row by the user and also show or call appropriate functions or methods according to the captured data of the corresponding wpf datagrid row.

In this particular example I will show a DataGrid with its data fetched from my MS SQL server database and  double clicking on each row, data will be shown in a WPF MessageBox to the users. We will have a separate WPF Button to fill the data into WPF DataGrid.

Create a new WPF window or page (you can also use existing one) and name it as Window1.xaml. Edit it as below :


<Window x:Class="WPFMaterialDesign.Window1"
Title="Window1" Height="500" Width="800">
<StackPanel Orientation="Vertical" >
<Button Name="btnFillGrid" Content="FILL GRID" Margin="5" MaxWidth="100" Click="btnFillGrid_Click"/>
<DataGrid Name="dataGrid" AutoGenerateColumns="False" CanUserAddRows="False"
Margin="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
RowBackground="#fff" FontWeight="Bold" Foreground="#525252"
ScrollViewer.CanContentScroll="True" Height="390" MaxHeight="390"
AlternatingRowBackground="#f2f2f2" BorderBrush="#000" BorderThickness="1"
<DataGridTextColumn Header="Id" Binding="{Binding Path='Id'}" IsReadOnly="True" />
<DataGridTextColumn Header="Product" Binding="{Binding Path='ProName'}" IsReadOnly="True" />
<DataGridTextColumn Header="Description" Binding="{Binding Path='ProDesc'}" IsReadOnly="True" />
<DataGridTextColumn Header="Date" Binding="{Binding Path='OnDate'}" IsReadOnly="True" />

WPF DataGrid MouseDoubleClick 01

And edit its C# code behind page as below :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Input;

namespace WPFMaterialDesign
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
public Window1()

private void btnFillGrid_Click(object sender, RoutedEventArgs e)
//This is sql connection string.....Database name is Parallelcodes
SqlConnection con = new SqlConnection(@"Data Source=VIBES\SQLEXPRESS;Initial Catalog=ParallelCodes;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select * from Producttbl", con);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
dataGrid.ItemsSource = dt.DefaultView;

catch (Exception ex)

private void dataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
if (sender != null)
DataGrid grid = sender as DataGrid;
if (grid != null && grid.SelectedItems != null && grid.SelectedItems.Count == 1)
//This is the code which helps to show the data when the row is double clicked.
DataGridRow dgr = grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem) as DataGridRow;
DataRowView dr = (DataRowView)dgr.Item;

String ProductName = dr[1].ToString();
String ProductDescription = dr[2].ToString();
MessageBox.Show("You Clicked : \r\nName : " + ProductName + "\r\nDescription : " + ProductDescription);


catch (Exception ex)

The dataGrid_MouseDoubleClick method will be called when the user double clicks a row on our WPF DataGrid row.

WPF DataGrid MouseDoubleClick 02

Thank You.

Also see :

WPF Datagrid with Button and Click Method

WPF Designing Material Design Tabs with Dragablz

WPF Button Style with Rounded Corners and Hover Effects

WPF Textbox With Rounded Corners

WPF Textbox Style – Changing Colors on Focus

WPF ComboBox SelectionChanged – SelectedItem Method