I would like to get all rows in order to check some rows that are the same in the database , I'm try using many solutions in StackOverlow but any of them work for me :
for (int i = 0; i < dataGrid.Items.Count; i++)
{
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator
.ContainerFromIndex(i);
}
This code return value cannot be null
Exception.
I have tried also this line of code and I'm got the same Exception ( the object currentitem was null ).
var currentItem = myDataGrid.SelectedItem as MyObject;
Complete Code:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
grid.ItemsSource = null;
grid.ItemsSource = work;
grid.Items.Refresh();
for (int i = 0; i < grid.Items.Count; i++) // My grid items count equals to 7
{
DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(i); // this value was null
var currentItem = grid.SelectedItem as object; // this value was null
}
}