locked
WPF DataGrid cell content by code RRS feed

  • Question

  • I need to read a cell content from defined row.
    The row I can get with:

    DataGridRow row1 = (DataGridRow)myDataGrid.ItemContainerGenerator.ContainerFromIndex(i);

    How I can get the value (TextBox) from column 4 in that row?

    I need the value from the DataGrid cell not from the dataset! And no XAML code - it has to be managed on codebehind.

    Thanks for answers
    Wednesday, July 8, 2009 1:46 PM

Answers

  • Hi mjeh,

    You can do something like this to get the content from the specified WPF DataGridViewCell:

     private void button1_Click(object sender, RoutedEventArgs e)
            {
                DataGridCell cell = GetCell(4, 1);
                TextBlock tb = cell.Content as TextBlock;
                MessageBox.Show(tb.Text);
                //....
            }
    
            public DataGridCell GetCell(int row, int column)
            {
                DataGridRow rowContainer = GetRow(row);
    
                if (rowContainer != null)
                {
                    DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
    
                    DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                    if (cell == null)
                    {
                        dataGrid1.ScrollIntoView(rowContainer, dataGrid1.Columns[column]);
                        cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                    }
                    return cell;
                }
                return null;
            }
    
            public DataGridRow GetRow(int index)
            {
                DataGridRow row = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(index);
                if (row == null)
                {
                    dataGrid1.UpdateLayout();
                    dataGrid1.ScrollIntoView(dataGrid1.Items[index]);
                    row = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(index);
                }
                return row;
            }
    
            public 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;
            }



    Best Regards,
    Zhi-Xin
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com .
    • Proposed as answer by Kenneth Haugland Tuesday, July 14, 2009 7:59 PM
    • Marked as answer by Zhi-Xin Ye Wednesday, July 15, 2009 11:58 AM
    Tuesday, July 14, 2009 3:44 PM

All replies

  • use DataRowView  class
    means
    put following line of code

    foreach (DataRowView item in dataGrid1.SelectedItems)
    {
    string test = item[4].ToString();
    }

    it wil give u value (TextBox) from column 4 in that row


    hope it solve your problem !!!!!!!!!!!


    Harshad..... Always 4 U
    Thursday, July 9, 2009 5:21 AM
  • Hi mjeh,

    You can do something like this to get the content from the specified WPF DataGridViewCell:

     private void button1_Click(object sender, RoutedEventArgs e)
            {
                DataGridCell cell = GetCell(4, 1);
                TextBlock tb = cell.Content as TextBlock;
                MessageBox.Show(tb.Text);
                //....
            }
    
            public DataGridCell GetCell(int row, int column)
            {
                DataGridRow rowContainer = GetRow(row);
    
                if (rowContainer != null)
                {
                    DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
    
                    DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                    if (cell == null)
                    {
                        dataGrid1.ScrollIntoView(rowContainer, dataGrid1.Columns[column]);
                        cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                    }
                    return cell;
                }
                return null;
            }
    
            public DataGridRow GetRow(int index)
            {
                DataGridRow row = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(index);
                if (row == null)
                {
                    dataGrid1.UpdateLayout();
                    dataGrid1.ScrollIntoView(dataGrid1.Items[index]);
                    row = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(index);
                }
                return row;
            }
    
            public 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;
            }



    Best Regards,
    Zhi-Xin
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com .
    • Proposed as answer by Kenneth Haugland Tuesday, July 14, 2009 7:59 PM
    • Marked as answer by Zhi-Xin Ye Wednesday, July 15, 2009 11:58 AM
    Tuesday, July 14, 2009 3:44 PM
  • Thanks a lot - it solve's the problem!

    Thanks all for answers
    Wednesday, July 15, 2009 12:21 PM
  • Very Good!!! It worked perfectly!

    Tks!!!

    Saturday, August 21, 2010 6:42 PM
  • Hi Zhi-Xin,

       The above post is an excellent peace of work actually i am also working on the same to retrive a cell value from the datagrid in wpf.But i am getting my ObservableCollection from the below mentioned code.

                       DataGridCell cell = GetCell(4, 1);
                       ComboBox cmbBx = cell.Content as ComboBox;

    i am getting null value into the cmbBx but i am getting the ObservableCollection from the cell.Content which i assaigned to my Datagrid as an ItemSource.

                               with the cell.Content am able to reach to the level which is mentioned below

                                DataGridCell->ContentPresenter->ContentPresenter

    but i should get my comboBox selectedItem values which is after the ConterPresenter.

                                   DataGridCell->ContentPresenter->ContentPresenter->MyControl:Controlname 

    I should get MyControl:ControlName content.

                      Is there any other way to retrive my ComboBox selectedValue. from cell?

                                

                      

    Wednesday, April 11, 2012 6:28 AM