C# DataGrid到DataRowView
当用户单击时,我需要从dataGrid向datarowview收取项目值,以获取第一个值“IdEmployee”并分配给变量 这是我的方法,问题是我的变量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
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());