2010-01-27 127 views
3

请帮助我,我试图从SelectionChangedEvent中的选定行中获取Cell [0]的值。WPF Toolkit DataGrid SelectionChanged获取单元格值

我只是设法得到很多不同的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的是不是一种选择.. ..

很多很多谢谢,这一直让我生气! 丹

回答

7

请,检查下面的代码会为你工作:

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; 
} 

希望这会有所帮助,至于

+0

完美的作品!我可以看到我出错的地方和原因! 非常感谢Serge! – 2010-01-28 09:20:14

+0

这真是太棒了!帮助了我很多! – 2011-07-10 08:43:40

+0

GetVisualChild 方法在哪里? – bugged87 2012-11-15 00:44:28

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

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

这将使你在DataGrid中的WPF当前选定行: -

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

现在获取单元格值只需写入dtr[0]dtr["ID"]

+0

此代码给我一个例外:无法将类型为“System.Data.Common.DataRecordInternal”的对象转换为键入“System.Data.DataRowView”。 – 2014-03-24 15:52:45

+0

我在VB.NET中使用了这个指令,并且工作完美: Dim dtr As DataRow = TryCast(DataGrid1.SelectedItem,DataRowView).Row – 2017-12-12 16:51:19

12

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

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

这个答案非常准确和简单。 – 2014-03-24 16:00:16

+1

我刚开始玩WPF,我认为这是最好的答案,它很简单,可读性强,而且工作完美。谢谢@Ayaz – theMarceloR 2014-04-07 10:45:50

+0

我开始使用这段代码并且很好,但是当我在数据网格中隐藏列时,它给了我们一些问题。我发布了另一个解决方案,可能有助于防止有人遇到同样的问题。谢谢! – 2014-04-12 20:20:21

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。即使在隐藏列时,这也适用于我。希望它也适用于你。

相关问题