2011-08-08 47 views

回答

0

這是我們可以得到一個特定的細胞

Object obj = GetCell(3).Content; 
        string cellContent = String.Empty; 
        if (obj != null) 
        { 
         if (obj is TextBox) 
          cellContent = ((TextBox)(obj)).Text; 
         else 
          cellContent = ((TextBlock)(obj)).Text; 
         } 




private DataGridCell GetCell(int column) 
    { 
     DataGridRow rowContainer = GetRow(); 

     if (rowContainer != null) 
     { 
      DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer); 

      // Try to get the cell but it may possibly be virtualized. 
      DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); 
      if (cell == null) 
      { 
       // Now try to bring into view and retreive the cell. 
       customDataGrid.UCdataGridView.ScrollIntoView(rowContainer, customDataGrid.UCdataGridView.Columns[column]); 
       cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); 
      } 
      return cell; 
     } 
     return null; 
    } 
0

嘗試使用此方法獲得所選行的列表:

IList rows = dg.SelectedItems; 

this related question

1

對於DataGrid,您可以通過CurrentCell屬性得到列:

DataGridCellInfo cellInfo = dataGrid.CurrentCell; 
DataGridColumn column=cellInfo.Column; 
+0

沒有名爲CurrentCell的此類屬性。 – Abhi

+0

@Abhishek:我們在談論System.Windows.Controls.DataGrid嗎?或者你的意思是System.Windows.Controls.Grid。這一個沒有選擇能力。它只是一個佈局容器。你能發佈你的網格控件的確切類型名稱嗎?也許我可以幫你... – HCL

相關問題