数据网格都被选择行“列中的值 [英] DataGrid get selected rows' column values

查看:206
本文介绍了数据网格都被选择行“列中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让在DataGrid中选择行中的每一列的值。这是我有:

I'm trying to get the values of each column of a selected row in a DataGrid. This is what I have:

private void dataGrid1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    DataGrid dg = sender as DataGrid;
    Console.WriteLine(dg.SelectedCells[0].ToString());
}

但是,这并不正常工作。如果我做了 SelectedCells.Count 然后我得到的列数正确,但我似乎无法真正得到所选行这些列中的值。我已经试过了,没有运气很长一段时间!这是我的XAML:

But this does not work. If I do a SelectedCells.Count then I get the correct number of columns but I cannot seem to actually get the values of these columns in the selected row. I've tried for quite a while with no luck! Here is my XAML:

<Grid>
    <DataGrid CanUserAddRows="True" AutoGenerateColumns="False" Height="200" HorizontalAlignment="Stretch" Margin="12,12,79,0" Name="dataGrid1" VerticalAlignment="Top" Width="389" DataContext="{Binding}" CanUserResizeColumns="False" CanUserResizeRows="False" HorizontalContentAlignment="Stretch" PreviewMouseDoubleClick="dataGrid1_PreviewMouseDoubleClick" CellEditEnding="dataGrid1_CellEditEnding">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding  Path=UserID}"
                                Header="User ID" Width="SizeToHeader" />
            <DataGridTextColumn Binding="{Binding  Path=UserName}"
                                Header="User ID" Width="SizeToHeader" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>

我在理想情况下想通过做这样的事情 rowData.UserID 来访问数据,但我似乎无法工作了。有很多的教程和帮助使用DataGridView的,但我没有用这个。

I would ideally like to access the data through doing something like rowData.UserID but I cannot seem to work it out. There are lots of tutorials and help for using DataGridView but I'm not using this.

推荐答案

更新时间:

要获得所选行试试:

IList rows = dg.SelectedItems;

您应该然后能够得到从行项目的列值。

You should then be able to get to the column value from a row item.

DataRowView row = (DataRowView)dg.SelectedItems[0];

然后:

row["ColumnName"];

这篇关于数据网格都被选择行“列中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆