2011-08-08 48 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

相关问题