获取选定行的值
我试图从 WPF 中的 DataGrid 获取选定的行值,事实证明这非常令人沮丧。不过,在 WinForms 中这很简单。
如何从 wpf 中的数据网格控件获取选定的或指定的单行值?
我使用了在网上找到的几个片段,它们都给了我整行的内容,包括列名!
我已经尝试过:
string value = dataGrid1.SelectedItems[0].ToString();
string value = dataGrid1.SelectedCells[dataGrid1.SelectedItems[0]].ToString();
我在 MSDN 的 wpf 控件部分查看过,但看不到如何在任何地方获取行值。
我不断得到的只是: { OrderId = d#dfuzJRo , UserId = 56, OrderTotal = , Freight = , DeliveryStatus = , OrderStatus = Pending, TransactionId = , OrderDate = 16/08/2011 9:12:00 PM }
- 其中这不是我所追求的。
我想在双击行时获取 OrderId。
我的 XAML 设置如下:
<DataGrid AutoGenerateColumns="True" Name="dataGrid1" DataContext="{Binding}" ItemsSource="{Binding}" HorizontalGridLinesBrush="#4D0090FF" VerticalGridLinesBrush="#A30091FF" AlternatingRowBackground="#200098FF" BorderThickness="0" ClipToBounds="False" MouseDoubleClick="dataGrid1_MouseDoubleClick" Margin="0,25,0,0" VerticalAlignment="Stretch"></DataGrid>
我成功获取数据库信息(基于 MSDN 教程),如下所示:
private void ViewOrders()
{
ObjectQuery<Order> orders = dEntities.Orders;
var query = from order in orders
orderby order.OrderStatus
select new
{
order.OrderId,
order.UserId,
order.OrderTotal,
order.Freight,
order.DeliveryStatus,
order.OrderStatus,
order.TransactionId,
order.OrderDate
};
dataGrid1.ItemsSource = query.ToList();
}
I am trying to get the selected row value from a DataGrid in WPF, and it's proving to be very frustrating. It's simple in WinForms, though.
How can I get the selected or a specified single row value from a datagrid control in wpf?
I have used several snippets i've found online, and all of them give me the contents of the whole row, including the column names!
I have tried:
string value = dataGrid1.SelectedItems[0].ToString();
string value = dataGrid1.SelectedCells[dataGrid1.SelectedItems[0]].ToString();
I've looked on MSDN under the wpf controls section but can't see how I can get a row value anywhere.
All I keep getting back is:{ OrderId = d#dfuzJRo , UserId = 56, OrderTotal = , Freight = , DeliveryStatus = , OrderStatus = Pending, TransactionId = , OrderDate = 16/08/2011 9:12:00 PM }
- which isn't what I'm after.
I would like to get the OrderId when I double-click on a Row.
My XAML is setup like:
<DataGrid AutoGenerateColumns="True" Name="dataGrid1" DataContext="{Binding}" ItemsSource="{Binding}" HorizontalGridLinesBrush="#4D0090FF" VerticalGridLinesBrush="#A30091FF" AlternatingRowBackground="#200098FF" BorderThickness="0" ClipToBounds="False" MouseDoubleClick="dataGrid1_MouseDoubleClick" Margin="0,25,0,0" VerticalAlignment="Stretch"></DataGrid>
And I am getting DB info successfully (based off of MSDN tutorial) like:
private void ViewOrders()
{
ObjectQuery<Order> orders = dEntities.Orders;
var query = from order in orders
orderby order.OrderStatus
select new
{
order.OrderId,
order.UserId,
order.OrderTotal,
order.Freight,
order.DeliveryStatus,
order.OrderStatus,
order.TransactionId,
order.OrderDate
};
dataGrid1.ItemsSource = query.ToList();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,简单的
SelectedItem
或SelectedItems[0]
将为您提供该行。但您的行是匿名类型,这使得获取OrderID
有点困难。简单的方法是简单地选择
order
项目并显示它。在网格中设置列定义以选择要显示的属性。另一种方法是定义一个
类 DisplayOrder
,然后更改 Linq 查询:在这两种情况下,您都可以简单地从网格访问
SelectedItem.OrderID
。OK, the simple
SelectedItem
orSelectedItems[0]
will give you the row. But your row is an anonymous type, that makes getting theOrderID
a little difficult.The simple approach is to simply select the
order
item and display that. Set up column definitions in the grid to select which properties to show.The alternative is to define a
class DisplayOrder
, and then change the Linq query:In both cases, you can simply access
SelectedItem.OrderID
from the grid.