简体   繁体   English

WPF Datagrid行索引始终为0

[英]WPF Datagrid row Index always is 0

I am trying to get current editing row index , but it will keep on return 0, I think is because the grid is not visualize yet , so I create a dispatcher to run the code , but still not working 我正在尝试获取当前的编辑行索引,但它将保持返回0,我认为是因为网格尚未可视化,所以我创建了一个调度程序来运行代码,但仍无法正常工作

Following is my Code 以下是我的代码

private void grdUser_RowEditingEnd(object sender, DataGridRowEditEndingEventArgs e )
{
        Dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(() =>
        {                
            int currentRowIndex = this.grdUser.ItemContainerGenerator.IndexFromContainer(this.grdUser.ItemContainerGenerator.ContainerFromItem(this.grdUser.CurrentItem));
            MessageBox.Show(currentRowIndex.ToString());

        }));
}

Is that Other way to get the edited row index ? 那是获取已编辑行索引的另一种方法吗?

You can use the eventargs and using that you can get index. 您可以使用eventargs并使用它可以获取索引。 You can refer the below code. 您可以参考以下代码。

private void grdUser_RowEditingEnd(object sender, DataGridRowEditEndingEventArgs e)
    {
        Dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(() =>
        {
            int currentRowIndex = this.grdUser.ItemContainerGenerator.IndexFromContainer(e.Row);
            MessageBox.Show(currentRowIndex.ToString());

        }));            
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM