2

Can you please tell how to get selected column name or index in WPF Grid.

Abhi
  • 5,501
  • 17
  • 78
  • 133

3 Answers3

5

For the DataGrid, the column you can get via the CurrentCell-property:

DataGridCellInfo cellInfo = dataGrid.CurrentCell;
DataGridColumn column=cellInfo.Column;
HCL
  • 36,053
  • 27
  • 163
  • 213
  • @Abhishek: Are we talking about System.Windows.Controls.DataGrid? Or do you mean System.Windows.Controls.Grid. This one has no selection capabilities. It's only a layout container. Can you post the exact type name of your Grid-Control. Maybe then I can help you... – HCL Aug 08 '11 at 13:53
0

Try this to get a list of rows selected:

IList rows = dg.SelectedItems;

From this related question.

Community
  • 1
  • 1
p.campbell
  • 98,673
  • 67
  • 256
  • 322
0

This is how we can get the value of a specific cell

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;
    }
Abhi
  • 5,501
  • 17
  • 78
  • 133