The final (and generally most user-friendly) method is to automatically maintain an empty row as the
last row in the DataGrid. The user can enter data for a new item in this row, and the DataGrid shouldautomatically add a new empty row once they do. Sadly, there is no built-in feature like this in the coreSilverlight DataGrid. You could add it yourself (all the source code for the DataGrid control is available inthe Silverlight Toolkit), but it’s not a great idea, as you’d be tying yourself to that particular version of theDataGrid control, and wouldn’t be able to (easily) take advantage of new features and bug fixes in futurereleases of Silverlight and the Silverlight Toolkit. You can, however, handle a number of events raised bythe DataGrid control and manage this automatically. The steps are as follows:1. Maintain a class-level variable that will reference the new item object:private object addRowBoundItem = null;2. Add a new item to the bound collection before (or shortly after) binding, andassign this item to the class-level variable. If binding the DataGrid directly to aDomainDataSource control, you would do this in the LoadedData event of theDomainDataSource control, like so:DomainDataSourceView view = productDataGrid.ItemsSource as DomainDataSourceView;
addRowBoundItem = new Product();view.Add(addRowBoundItem);3. Handle the RowEditEnded event of the DataGrid control. If the row beingcommitted is the empty row item that was edited (you can get the item in thecollection that it is bound to from the DataContext property of the row), thenit’s time to add a new item to the end of the bound collection, ensure it isvisible, select it, and put it in edit mode. For example:private void productDataGrid_RowEditEnded(object sender,
DataGridRowEditEndedEventArgs e){ if (e.EditAction == DataGridEditAction.Commit){ if (e.Row.DataContext == addRowBoundItem){ DomainDataSourceView view =productDataGrid.ItemsSource as DomainDataSourceView;addRowBoundItem = new Product();view.Add(addRowBoundItem);productDataGrid.SelectedItem = addRowBoundItem;productDataGrid.CurrentColumn = productDataGrid.Columns[0];
productDataGrid.ScrollIntoView(addRowBoundItem,productDataGrid.CurrentColumn);productDataGrid.BeginEdit();}}}4. Remember to always delete the last item in the collection before submitting
the changes back to the server (as it will always be the item representing thenew row):DomainDataSourceView view = productDataGrid.ItemsSource as DomainDataSourceView;view.Remove(addRowBoundItem);