Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGrid到DataRowView_C#_Wpf_Datagrid_Datarow - Fatal编程技术网

C# DataGrid到DataRowView

C# DataGrid到DataRowView,c#,wpf,datagrid,datarow,C#,Wpf,Datagrid,Datarow,当用户单击时,我需要从dataGrid向datarowview收取项目值,以获取第一个值“IdEmployee”并分配给变量 这是我的方法,问题是我的变量dataRowView为空 我怎样才能解决这个问题 private void _employeedataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e) { DataRowView dataRowView = _employeedataGrid.CurrentCel

当用户单击时,我需要从dataGrid向datarowview收取项目值,以获取第一个值“IdEmployee”并分配给变量

这是我的方法,问题是我的变量dataRowView为空

我怎样才能解决这个问题

private void _employeedataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    DataRowView dataRowView = _employeedataGrid.CurrentCell.Item as DataRowView;
    var idEmployee = Convert.ToInt32(dataRowView.Row[0]);

    .......
} 

这是因为
\u employeedataGrid.CurrentCell.Item
不能强制转换为
数据行视图
。为什么不试试
CurrentRow
而不是
CurrentCell

private void _employeedataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    DataRowView dataRowView = _employeedataGrid.CurrentRow.Item as DataRowView;
    var idEmployee = Convert.ToInt32(dataRowView.Row[0]);
    .......
} 
这就是你需要的

DataRowView view = _employeedataGrid.Items[_employeedataGrid.SelectedIndex] as DataRowView;

只要选中该项,就可以直接获取每列的值,而无需经过循环。列1将有一个索引行。ItemArray[0],列2将有一个索引行。ItemArray[1]等等。这一切都取决于您的任务

var EmpID = Convert.ToInt32(((DataRowView)_employeedataGrid.SelectedItem).Row.ItemArray[0].ToString());

我正在使用Wpf,没有“CurrentRow”选项。。我能做什么?嗨。我在WPF中也有同样的问题?你已经解决了问题吗?考虑编辑你的问题,添加一些描述你的答案如何解决问题的文本,它会使你的答案对原来的海报和其他人更有用。
var EmpID = Convert.ToInt32(((DataRowView)_employeedataGrid.SelectedItem).Row.ItemArray[0].ToString());