获取选定行的值

发布于 2024-12-01 01:37:20 字数 1736 浏览 0 评论 0原文

我试图从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

公布 2024-12-08 01:37:20

好的,简单的 SelectedItemSelectedItems[0] 将为您提供该行。但您的行是匿名类型,这使得获取 OrderID 有点困难。

简单的方法是简单地选择 order 项目并显示它。在网格中设置列定义以选择要显示的属性。

另一种方法是定义一个 类 DisplayOrder,然后更改 Linq 查询:

   select new DisplayOrder
   {
       order.OrderId,
       ...
   };

在这两种情况下,您都可以简单地从网格访问 SelectedItem.OrderID

  var row = (DisplayOrder) datagrid1.SelectedItem;  // or (Order)
  int Id = row.OrderID;

OK, the simple SelectedItem or SelectedItems[0] will give you the row. But your row is an anonymous type, that makes getting the OrderID 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:

   select new DisplayOrder
   {
       order.OrderId,
       ...
   };

In both cases, you can simply access SelectedItem.OrderID from the grid.

  var row = (DisplayOrder) datagrid1.SelectedItem;  // or (Order)
  int Id = row.OrderID;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文