【问题标题】:WPF Toolkit DataGrid SelectionChanged Getting Cell ValueWPF Toolkit DataGrid SelectionChanged 获取单元格值
【发布时间】:2010-01-27 17:30:58
【问题描述】:

请帮助我,我试图从 SelectionChangedEvent 中的选定行中获取 Cell[0] 的值。

我只是设法获得许多不同的 Microsoft.Windows.Controls,我希望我错过了一些愚蠢的东西。

希望我能从这里得到一些帮助......

    private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Microsoft.Windows.Controls.DataGrid _DataGrid = sender as Microsoft.Windows.Controls.DataGrid;
    }

我希望它会像......

_DataGrid.SelectedCells[0].Value;

但是 .Value 不是一个选项....

非常感谢,这让我发疯了! 丹

【问题讨论】:

    标签: c# wpf datagrid wpfdatagrid


    【解决方案1】:

    更少的代码,它的工作原理。

    private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataGrid dataGrid = sender as DataGrid;
            DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(dataGrid.SelectedIndex);
            DataGridCell RowColumn = dataGrid.Columns[ColumnIndex].GetCellContent(row).Parent as DataGridCell;
            string CellValue = ((TextBlock)RowColumn.Content).Text;
        }
    

    ColumnIndex 是你想知道的列的索引。

    【讨论】:

    • 这个答案非常准确和简单。
    • 我刚开始使用 WPF,我认为这是最好的答案,它简单、易读且运行良好。谢谢@Ayaz
    • 我开始使用这段代码并且很好,但是在我的数据网格上隐藏列时它给了我一些问题。我发布了另一个解决方案,如果有人遇到与我相同的问题,它可能会有所帮助。谢谢!
    【解决方案2】:

    请检查以下代码是否适合您:

    private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        DataGrid dataGrid = sender as DataGrid;
        if (e.AddedItems!=null && e.AddedItems.Count>0)
        {
            // find row for the first selected item
            DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(e.AddedItems[0]);
            if (row != null)
            {
                DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
                // find grid cell object for the cell with index 0
                DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(0) as DataGridCell;
                if (cell != null)
                {
                    Console.WriteLine(((TextBlock)cell.Content).Text);
                }
            }
        }
    }
    
    static T GetVisualChild<T>(Visual parent) where T : Visual
    {
        T child = default(T);
        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null) child = GetVisualChild<T>(v);
            if (child != null) break;
        }
        return child;
    }
    

    希望这会有所帮助,问候

    【讨论】:

    • 完美运行!我可以看到我出错的地方和原因!非常感谢 Serge!
    • 这个 GetVisualChild 方法在哪里?
    • 太棒了!最后我想出了如何让我的数据网格正常工作...谢谢 Serge!
    【解决方案3】:

    这将为您提供 WPF 中 DataGrid 中当前选定的行:-

    DataRow dtr = ((System.Data.DataRowView)(DataGrid1.SelectedValue)).Row;
    

    现在要获取单元格值,只需编写dtr[0]dtr["ID"] 等。

    【讨论】:

    • 这段代码给了我一个异常:无法将“System.Data.Common.DataRecordInternal”类型的对象转换为“System.Data.DataRowView”类型。
    • 我在 VB.NET 中使用了这条指令并且效果很好:Dim dtr As DataRow = TryCast(DataGrid1.SelectedItem, DataRowView).Row
    【解决方案4】:

    由于您使用的是“SelectionChanged”,因此您可以将发件人用作数据网格:

    DataGrid dataGrid = sender as DataGrid;
    DataRowView rowView = dataGrid.SelectedItem as DataRowView;
    string myCellValue = rowView.Row[0].ToString(); /* 1st Column on selected Row */
    

    我尝试了此处发布的答案并且很好,但是在开始隐藏 DataGrid 中的列时给了我问题。即使隐藏列,这个也对我有用。希望它也对你有用。

    【讨论】:

      【解决方案5】:
      private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          DataGrid _DataGrid = sender as DataGrid;
      
          string strEID = _DataGrid.SelectedCells[0].Item.ToString(); 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-31
        • 2011-07-24
        • 1970-01-01
        • 1970-01-01
        • 2011-12-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多