0

I need to get each value of each cell in a row. First when I add the data to the new row. The data goes away after hitting enter - however it is in my ObservableCollection.

I have an ObservableCollection<object> which I set up so that I can re-use the Datagrid. For instance I have an ObservableCollection<object> which holds multiple CommissionOperator (db/linq object) objects in it. After I insert the new data into the grid, I get ,lets say, 10 CommissionOperator objects in my ObservableCollection and one object object in my ObservableCollection.

Now, I am trying to figure out how to get the value from the Datagrid. So I did some research and came up with this (from a question I cant find at the moment to link):

//in a class that inherits from DataGrid
     public Microsoft.Windows.Controls.DataGridCell GetCell( int column )
            {
                Microsoft.Windows.Controls.DataGridRow rowContainer = GetRow(this.SelectedIndex);

                if (rowContainer != null)
                {
                DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>( rowContainer );

                // Try to get the cell but it may possibly be virtualized.
                Microsoft.Windows.Controls.DataGridCell cell = (Microsoft.Windows.Controls.DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex( column );
                if (cell == null)
                {
                    // Now try to bring into view and retreive the cell.
                    this.ScrollIntoView( rowContainer, this.Columns[column] );
                    cell = (Microsoft.Windows.Controls.DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex( column );
                }
                return cell;
            }
            return null;
        }

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

So I end up doing something like this:

((TextBox) (RGVDataSelector.GetCell(1).Content)).Text

Now I get an error saying that I cannot cast TextBlock into a TexBox and the TextBlock.Text is empty...

Im just trying to figure out how to get the data from the DataGrid when I enter data into a new row...

Here is how my ObservableCollection looks after adding the new row:

enter image description here

Here is how the DataGrid is during adding (just as a reference)

enter image description here

Here is the DataGrid after insert

enter image description here

Which I figure if I can just get the data from out of the cell that I can rebind the DataGrid or update the bindings or something to get it to show the right data.

Could this be happening because the data is disappearing from my DataGrid that I cannot actually get the data from that cell?

jharr100
  • 1,449
  • 24
  • 51
  • You don't need any of this. You don't "read data from the UI" in WPF because [UI is NOT data](http://stackoverflow.com/a/14382137/643085). Create a proper ViewModel or else read your data from your data items instead. – Federico Berasategui Dec 10 '13 at 19:05
  • I have a proper ViewModel which is where my `SelectionData` `ObservableCollection` is... however I cannot get the value from of lets say `SelectionData[10]` from the pic of the `ObservableCollection` it has no properties - so I cant use reflection to get the value.... – jharr100 Dec 10 '13 at 19:09
  • Change your collection to a strongly typed collection, not a collection of `object`. Otherwise have the ViewModel create new items, not the View. The View is not responsible for managing data items, only for showing them. – Federico Berasategui Dec 10 '13 at 19:11
  • Thats the issue - I want to be able to reuse the grid with as little code as possible...by creating a strongly typed collection I wont have the option of doing this. What do you mean by have the ViewModel create new items (how can I do that without the new data being added by the user)? – jharr100 Dec 10 '13 at 19:23

0 Answers0