Xceed DataGrid for WPF v7.2 Documentation
Manually handling the insertion process

The following example demonstrates how to manually handle the insertion process of a new item into a collection.

XAML
Copy Code
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
     xmlns:local="clr-namespace:Xceed.Wpf.Documentation">
  <Grid.Resources>
    <xcdg:DataGridCollectionViewSource x:Key="cvs_persons"
                                       Source="{Binding Source={x:Static Application.Current},
                                                        Path=PersonList}"
                                       CreatingNewItem="CollectionView_CreatingNewItem"
                                       CommittingNewItem="CollectionView_CommittingNewItem"
                                       CancelingNewItem="CollectionView_CancelingNewItem"/>
  </Grid.Resources>
  <xcdg:DataGridControl x:Name="PersonsGrid"
                        ItemsSource="{Binding Source={StaticResource cvs_persons}}">
     <xcdg:DataGridControl.View>
        <xcdg:TableView>
           <xcdg:TableView.FixedHeaders>
              <DataTemplate>
                 <xcdg:InsertionRow/>
              </DataTemplate>
           </xcdg:TableView.FixedHeaders>
        </xcdg:TableView>
     </xcdg:DataGridControl.View>
  </xcdg:DataGridControl>
</Grid>

The following code provides the implementation of the CreatingNewItem, CommittingNewItem, and CancelingNewItem events.

VB.NET
Copy Code
Private Sub CollectionView_CreatingNewItem( ByVal sender As Object, _
                                            ByVal e As DataGridCreatingNewItemEventArgs )
  e.NewItem = New Person( Person.AutoIncrementID, String.Empty, String.Empty, -1 )
  e.Handled = True
End Sub
Private Sub CollectionView_CommittingNewItem( ByVal sender As Object, _
                                              ByVal e As DataGridCommittingNewItemEventArgs )
  Dim source As List( Of Person ) = CType( e.CollectionView.SourceCollection, List( Of Person ) )
  source.Add( CType( e.Item, Person ) )
  Person.AutoIncrementID = Person.AutoIncrementID + 1
  ' the new item is always added at the end of the list.
  e.Index = source.Count - 1
  e.NewCount = source.Count
  e.Handled = True
End Sub
Private Sub CollectionView_CancelingNewItem( ByVal sender As Object, _
                                             ByVal e As DataGridItemHandledEventArgs )
  ' Manually handling the insertion of new items requires that the CreatingNewItem,
  ' CommitingNewItem, and CancelingNewItem events must all be handled even if nothing
  ' is done in the event.
  e.Handled = True
End Sub
C#
Copy Code
private void CollectionView_CreatingNewItem( object sender, DataGridCreatingNewItemEventArgs e )
{
 e.NewItem = new Person( Person.AutoIncrementID, string.Empty, string.Empty, -1 );
 e.Handled = true;
}
private void CollectionView_CommittingNewItem( object sender, DataGridCommittingNewItemEventArgs e )
{
 List<Person> source = e.CollectionView.SourceCollection as List<Person>;
 source.Add( ( Person )e.Item );
 Person.AutoIncrementID = Person.AutoIncrementID + 1;
 // the new item is always added at the end of the list.     
 e.Index = source.Count - 1;
 e.NewCount = source.Count;
 e.Handled = true;
}
private void CollectionView_CancelingNewItem( object sender, DataGridItemHandledEventArgs e )
{
 // Manually handling the insertion of new items requires that the CreatingNewItem,
 // CommitingNewItem, and CancelingNewItem events must all be handled even if nothing
 // is done in the event.
 e.Handled = true;
}