c# - 获取数据网格中所选项目的行索引

标签 c# wpf

我正在尝试获取绑定(bind)到 DataTable 的数据网格中所选项目的行索引。

这是我的尝试(基于 this SO answer ):

private void ShowRowIndex_Btn(object sender, RoutedEventArgs e)
{
    int editedRowIndex = myDataGrid.Items.IndexOf(myDataGrid.CurrentItem);
    MessageBox.Show(editedRowIndex.ToString());
}

<DataGrid CellEditEnding="PriceListDG_CellEditEnding"  RowEditEnding="MyDataGrid_RowEditEnding" Name="priceListDataGrid" />

不幸的是,我总是得到 -1 作为结果。

最佳答案

如果你想获取当前正在编辑的行的索引,你可以直接在你的 RowEditEnding 事件中进行:

private void OnRowEditEnding(object sender, .DataGridRowEditEndingEventArgs e)
{
    var index = e.Row.GetIndex();
}

关于c# - 获取数据网格中所选项目的行索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53388800/

相关文章:

c# - 使用 Swagger 向端点添加注释

c# - 通过依赖注入(inject)进行多重导航控制

c# - 如何强制 WPF ListView 重新查询它的 ItemSsource?

c# - 激活作为 AutomationElement 检索的 TabControl 中的选项卡

c# - 不变的还是不可变的?

C# 替换 docx 中的文本字符串

c# - 什么是端点发现?

c# - 异步 WPF 命令

c# - Wpf 数据虚拟化

WPF 使用样式从嵌套元素中绑定(bind)到父属性