简体   繁体   English

从WPF Datagrid中的选定行获取价值

[英]Get value from selected row in WPF Datagrid

I have a DataGrid in my WPF projects 我的WPF项目中有一个DataGrid

在此处输入图片说明

As you can see i'm able to select a row. 如您所见,我能够选择一行。 I've made a double click method where i get the selected row. 我做了一个双击方法,在其中我得到了选定的行。 i want get just the ID part of that row. 我只想获取该行的ID部分。
This is how my method looks like 这就是我的方法的样子

在此处输入图片说明

private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
    DataGridRow = sender as DataGridRow;
}

How to I just get the cell where I put the ID in it? 我如何才能获得将ID放入其中的单元格?

Many thanks in advance 提前谢谢了

This is the code I used to get the ID of the double clicked row. 这是我用来获取双击行ID的代码。 I used an exploit of the double click (When you double click a row, you also select it). 我使用了双击功能(当您双击一行时,您也选择了它)。 In my case, the column containing the id was the first (Row[0]) 就我而言,包含id的列是第一列(Row [0])

    private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
    {
        DataRowView dataRowView = (DataRowView)yourDataGridView.SelectedItem;
        int ID = Convert.ToInt32(dataRowView.Row[0]);
    }

If you are showing both XAML and cs code then only we can find the proper solution. 如果同时显示XAML和CS代码,则只有我们才能找到合适的解决方案。 Now I am assuming that you are displaying the contents by using binding from an observable collection of any class type. 现在,我假设您正在通过使用任何类类型的可观察集合中的绑定来显示内容。 So you can easily get the ID field by, 这样您就可以轻松获得ID字段,

private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
    ClassName classObj = dataGridName.SelectedItem as ClassName;
    string id = classObj.ID;
}

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

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