如何在DataGrid上获取单击/选定单元格的索引? 我的DataGrid列自动生成,我不想使用任何DataTemplate。
<DataGrid ItemsSource="{Binding Table, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, IsAsync=True}"
AutoGenerateColumns="True">
</DataGrid>
DataGrid x = (DataGrid)this.FindName("myDataGrid");
var index = x.SelectedIndex;
还有其他有用的属性:
x.CurrentColumn;
x.CurrentItem;
x.SelectedItem;
x.SelectedValue;
这是我发现的解决方案,当选择单元是“单元格”并且您需要遍历所选单元格时,获取行和列索引。我有一个只有textcolumn的DataGrid,以及一个数据表(从csv文件中创建)作为itemssource。
For Each cell As DataGridCellInfo In dataGrid1.SelectedCells
MsgBox(cell.Column.DisplayIndex)
MsgBox(dataGrid1.Items.IndexOf(cell.Item))
Next