I have a wpf datagrid with the setting CanUserAddRows set to true. This will add a blank empty row to the datagrid and when the user double clicks on the row it will zero out all the properties and add it to the collection/itemsource(ObservableCollection). The problem is when the empty row zeros out and adds it to the collection, I need another blank row to be added and ready to use as soon as the row before is added and zeroed out. Instead a new blank row won't show on the datagrid until i select a new row(selectionchange). Hope this is clear what im asking. Any ideas on how this can be fixed? Thx for any input.
<DataGrid Grid.Row="1" RowHeaderWidth="0" BorderBrush="Black" ItemsSource="{Binding MyCollection, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" SelectedItem="{Binding SelectedRow, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Height="280" Focusable="True" CanUserAddRows="True">
<DataGridTextColumn Binding="{Binding Weight}" Header="Tare Lbs." Width="70" />
<DataGridTextColumn Binding="{Binding Bu, UpdateSourceTrigger=PropertyChanged}" Header="Gross Bu." Width="70" />
If you call DataGrid.CommitEdit() it will finish with the adding of the new row and create the new blank row for you. We do it in DataGrid.CurrentCellChanged().
XAML
<DataGrid x:Name="theGrid" ItemsSource="{Binding MyCollection, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" SelectedItem="{Binding SelectedRow, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" CanUserAddRows="True" CurrentCellChanged="theGrid_CurrentCellChanged">
Code Behind:
private void theGrid_CurrentCellChanged(object sender, EventArgs e)
{
DataGrid grid = sender as DataGrid;
IEditableCollectionView items = (IEditableCollectionView)grid.Items;
if (items != null && items.IsAddingNew) {
// Commit the new row as soon as the user starts editing it
// so we get a new placeholder row right away.
grid.CommitEdit(DataGridEditingUnit.Row, false);
}
}