3

Please help me, Im trying to get the value of Cell[0] from the selected row in a SelectionChangedEvent.

I am only managing to get lots of different Microsoft.Windows.Controls and am hoping im missing something daft.

Hoping I can get some help from here...

    private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Microsoft.Windows.Controls.DataGrid _DataGrid = sender as Microsoft.Windows.Controls.DataGrid;
    }

I was hoping it would be something like...

_DataGrid.SelectedCells[0].Value;

However .Value isn't an option....

Many many thanks this has been driving me mad! Dan

Dan Bater
  • 303
  • 2
  • 6
  • 13

5 Answers5

17

Less code, and it works.

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 is the index of the column you want to know.

Ayaz Alifov
  • 8,334
  • 4
  • 61
  • 56
  • 1
    This answer is very accurate and simple. – Exel Gamboa Mar 24 '14 at 16:00
  • 1
    I just started playing with WPF and I think this is the best answer, it's simple, readable and it worked perfectly. Thanks @Ayaz – the_marcelo_r Apr 07 '14 at 10:45
  • I started to use this code and was nice, but it gave me problems when hiding columns on my Data Grid. I posted another solution that might help in case someone have the same problem that I had. Thanks! – Exel Gamboa Apr 12 '14 at 20:20
10

pls, check if code below would work for you:

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

hope this helps, regards

serge_gubenko
  • 20,186
  • 2
  • 61
  • 64
3

This will give you the current selected row in the DataGrid in WPF:-

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

Now to get the cell value just write dtr[0], dtr["ID"], etc.

  • This code gives me an exception: Unable to cast object of type 'System.Data.Common.DataRecordInternal' to type 'System.Data.DataRowView'. – Exel Gamboa Mar 24 '14 at 15:52
  • I used this instruction in VB.NET and worked perfectly: Dim dtr As DataRow = TryCast(DataGrid1.SelectedItem, DataRowView).Row – Andrea Antonangeli Dec 12 '17 at 16:51
3

Since you are using the "SelectionChanged", you can use the sender as a Data Grid:

DataGrid dataGrid = sender as DataGrid;
DataRowView rowView = dataGrid.SelectedItem as DataRowView;
string myCellValue = rowView.Row[0].ToString(); /* 1st Column on selected Row */

I tried the answers posted here and were good, but gave me problems when started to hide columns in the DataGrid. This one works for me even when hiding columns. Hope it works for you too.

Exel Gamboa
  • 936
  • 1
  • 14
  • 25
1
private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DataGrid _DataGrid = sender as DataGrid;

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