Search code examples
wpfdatagrid

WPF : Get index of clicked / selected cell on DataGrid


How do I get index of clicked / selected cell on DataGrid ?
My DataGrid columns generated automatically and I don't want to use any DataTemplate .

<DataGrid ItemsSource="{Binding Table, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, IsAsync=True}"
          AutoGenerateColumns="True">
</DataGrid>

Solution

  • DataGrid x = (DataGrid)this.FindName("myDataGrid");
    var index = x.SelectedIndex;
    

    There are also other usefull properties:

    x.CurrentColumn;
    x.CurrentItem;
    x.SelectedItem;
    x.SelectedValue;