2010-01-27 128 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。即使在隱藏列時,這也適用於我。希望它也適用於你。

相關問題