42

I need to programmatically create a DataGrid and need to add a double click row event to it. How is this done in C#? I found this;

myRow.MouseDoubleClick += new RoutedEventHandler(Row_DoubleClick);

Although this does not work for me since I am binding the DataGrid.ItemsSource to a collection and not manually adding in the rows.

Xaphann
  • 3,195
  • 10
  • 42
  • 70
  • 5
    Wouldn't it be easiest to subscribe to `myGrid.MouseDoubleClick` and then in the event handler check if the click happened on a row (or which row is selected)? – floele Apr 01 '14 at 15:37
  • Unless by "programmatically create a DataGrid" (which, btw, taken literally, means there would be no DataGrid Element in the XAML File), you just mean "binding the DataGrid.ItemsSource to a collection and not manually adding in the rows", which I suspect is what you meant esp. based on you marking Rohit Vats's A on "Apr 1 '14 at 16:37" as the Accepted A, this Q is a dupe of "https://stackoverflow.com/questions/3120616/wpf-datagrid-selected-row-clicked-event". – Tom Mar 04 '20 at 23:54
  • Does this answer your question? [WPF datagrid selected row clicked event ?](https://stackoverflow.com/questions/3120616/wpf-datagrid-selected-row-clicked-event) – Tom Mar 04 '20 at 23:56

1 Answers1

94

You can do that in XAML by adding default style for DataGridRow under its resources section and declare event setter over there:

<DataGrid>
    <DataGrid.Resources>
        <Style TargetType="DataGridRow">
            <EventSetter Event="MouseDoubleClick" Handler="Row_DoubleClick"/>
        </Style>
    </DataGrid.Resources>
</DataGrid>

OR

In case want to do it in code behind. Set x:Name on grid, create style programatically and set the style as RowStyle.

<DataGrid x:Name="dataGrid"/>

and in code behind:

Style rowStyle = new Style(typeof(DataGridRow));
rowStyle.Setters.Add(new EventSetter(DataGridRow.MouseDoubleClickEvent,
                         new MouseButtonEventHandler(Row_DoubleClick)));
dataGrid.RowStyle = rowStyle;

AND

There is example of event handler:

  private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
  {
     DataGridRow row = sender as DataGridRow;
     // Some operations with this row
  }
Vizor
  • 396
  • 3
  • 14
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • Thanks, I will retry. – Gsv Jul 14 '17 at 00:58
  • Thanks!! If somebody needs this code for **PowerShell** copy this code: https://gist.github.com/PatrickGrub/94841e76b4ba45beb28a4fab94c29120 – Patrick Jul 10 '18 at 11:43
  • This only answers the "add a double click row event to it" part of the OP's Q (unless of course, by "programmatically create a DataGrid", he just means "binding the DataGrid.ItemsSource to a collection and not manually adding in the rows, which I suspect is what he meant).". I'm just saying, that literally speaking, "programmatically create a DataGrid" means there would be no `DataGrid` Element in the XAML File. – Tom Mar 04 '20 at 23:46
  • Just as note, sender is a DataGrid, not a row. You may use DataGrid.SelectedIndex or any other accessor to manage the action on your handler – Noman_1 Oct 25 '20 at 20:38