WPF Toolkit DataGrid SelectionChanged Getting Cell Value

50,402

Solution 1

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

Solution 2

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.

Solution 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.

Solution 4

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.

Solution 5

private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DataGrid _DataGrid = sender as DataGrid;

    string strEID = _DataGrid.SelectedCells[0].Item.ToString(); 
}
Share:
50,402
Dan Bater
Author by

Dan Bater

"Daniel Bater is a Research Assistant at i-DAT who designs and develops applications for creative projects. He is currently developing versions of the LiquidReader in collaboration with Performance Research Journal (Liquid Reader™ v4.0. ‘Digital Resources’ DVD supplements Performance Research Vol.11, No.4 ‘Digital Resources’ (2006)) and the Liquid Reader Reader, to be published in early 2008. Dan is also developing content and the content management system for the ‘The Green Screen’ for the Center for Sustainable Futures and i-DAT." 2007

Updated on April 13, 2020

Comments

  • Dan Bater
    Dan Bater about 4 years

    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
    Dan Bater over 14 years
    Works perfect! I can see where and why I was going wrong! Many Thanks Serge!
  • bugged87
    bugged87 over 11 years
    Where is this GetVisualChild<T> method?
  • Exel Gamboa
    Exel Gamboa about 10 years
    This code gives me an exception: Unable to cast object of type 'System.Data.Common.DataRecordInternal' to type 'System.Data.DataRowView'.
  • Exel Gamboa
    Exel Gamboa about 10 years
    This answer is very accurate and simple.
  • the_marcelo_r
    the_marcelo_r about 10 years
    I just started playing with WPF and I think this is the best answer, it's simple, readable and it worked perfectly. Thanks @Ayaz
  • Exel Gamboa
    Exel Gamboa about 10 years
    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!
  • MaxBauer416
    MaxBauer416 over 8 years
    Great! Finally i figured out how to get my datagrids working...thank you Serge!
  • Andrea Antonangeli
    Andrea Antonangeli over 6 years
    I used this instruction in VB.NET and worked perfectly: Dim dtr As DataRow = TryCast(DataGrid1.SelectedItem, DataRowView).Row