WPF 工具包中的 DataGrid 选择更改:获取单元格值

4

请帮我,我正在尝试从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不是一个选项....

非常感谢,这让我头疼了很久! 丹

5个回答

17

代码更少,功能正常。

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是您想要知道的列的索引。


1
这个答案非常准确和简单。 - Exel Gamboa
1
我刚开始尝试使用WPF,我认为这是最好的答案,它简单易懂,而且完美地运行了。谢谢@Ayaz。 - the_marcelo_r
我开始使用这段代码,感觉不错,但是当我在我的数据网格上隐藏列时,它给了我一些问题。我发布了另一个解决方案,以防有人遇到和我一样的问题。谢谢! - Exel Gamboa

11

请检查下面的代码是否适用于您:

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! - Dan Bater
这个 GetVisualChild<T> 方法在哪里? - bugged87
太棒了,我终于弄清楚如何让我的数据表格工作了...谢谢Serge! - MaxBauer416

3
这将为您提供WPF中DataGrid中当前所选行的内容:-
DataRow dtr = ((System.Data.DataRowView)(DataGrid1.SelectedValue)).Row;

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

这段代码给我一个异常:无法将类型为“System.Data.Common.DataRecordInternal”的对象强制转换为类型“System.Data.DataRowView”。 - Exel Gamboa
我在VB.NET中使用了这个指令,它完美地工作了:Dim dtr As DataRow = TryCast(DataGrid1.SelectedItem, DataRowView).Row - Andrea Antonangeli

3

由于您正在使用“SelectionChanged”,因此可以将发送者用作数据网格:

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

我尝试了这里发布的答案,效果不错,但当我开始隐藏DataGrid中的列时,它给我带来了问题。这个方法即使在隐藏列时也适用于我。希望它对你也有用。


1
private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DataGrid _DataGrid = sender as DataGrid;

    string strEID = _DataGrid.SelectedCells[0].Item.ToString(); 
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接