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:
Here is how the DataGrid
is during adding (just as a reference)
Here is the DataGrid
after insert
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?