1

Can you please tell , how to get and set the value of a cell in WPF DataGrid by providing columnidnex and row index

Thanks

Abhi
  • 5,501
  • 17
  • 78
  • 133
  • 1
    http://stackoverflow.com/questions/1295843/wpf-datagrid-how-do-you-get-the-content-of-a-single-cell see if it helps Abhishek – Devjosh Aug 18 '11 at 07:43

2 Answers2

1

This is not as easy as it should be in WPF, it is all about selecting visual children (the WPF 'Visual' object type). This blog post explains quite well how to accomplish it. Google it and you will probably find a lot more.

Edwin de Koning
  • 14,209
  • 7
  • 56
  • 74
1
 // Getting Data from Grid Cell 
 string cellContent = ((TextBox)(GetCell(3).Content)).Text; //As I want the value of 3 column

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

 public DataGridRow GetRow()
        {
            DataGridRow row = (DataGridRow)customDataGrid.UCdataGridView.ItemContainerGenerator.ContainerFromIndex(_currentRowIndex);
            if (row == null)
            {
                // May be virtualized, bring into view and try again.
                customDataGrid.UCdataGridView.UpdateLayout();
                customDataGrid.UCdataGridView.ScrollIntoView(customDataGrid.UCdataGridView.Items[_currentRowIndex]);
                row = (DataGridRow)customDataGrid.UCdataGridView.ItemContainerGenerator.ContainerFromIndex(_currentRowIndex);
            }
            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;
        }

// Setting Data to Grid Cell
if (GetCell(3).Content is TextBlock) // if grid cell is not editable
{
      ((TextBlock)(GetCell(3).Content)).Text = "sometext";
}
else // TextBox  - if grid cell is editable
{
        ((TextBox)(GetCell(3).Content)).Text = "sometext";
}
Edwin de Koning
  • 14,209
  • 7
  • 56
  • 74
Abhi
  • 5,501
  • 17
  • 78
  • 133