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>
DataGrid x = (DataGrid)this.FindName("myDataGrid");
var index = x.SelectedIndex;
There are also other usefull properties:
x.CurrentColumn;
x.CurrentItem;
x.SelectedItem;
x.SelectedValue;
This is the solution I found, when selection unit is "cell" and you need to loop through the selected cells, getting row and column index. I have a DataGrid with textcolumn only, and a datatable (creted from a csv file) as itemssource.
For Each cell As DataGridCellInfo In dataGrid1.SelectedCells
MsgBox(cell.Column.DisplayIndex)
MsgBox(dataGrid1.Items.IndexOf(cell.Item))
Next
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With