I have a CollectionViewSource (cvs) which has strongly typed DataTable as it's source. Cvs.View is set as DataGrid's ItemsSource. I want to update, insert and delete data from a database based on changes in DataGrid. I have successfully done update, and i have an idea for delete, but for insert i have some problems. I tried to do it by handling CurrentChanging event of cvs.View but row state is always Detached and it should be Added. Here is my code:
private void View_CurrentChanging(object sender, CurrentChangingEventArgs e)
{
if (cvs.View.CurrentItem != null)
{
var dataRow = ((cvs.View.CurrentItem as DataRowView).Row) as MyDataSet.MyTableRow;
if (dataRow.HasChanges())
{
//do update - works
}
dataRow.EndEdit(); // without this line RowState is Unchanged when it should be Added
if (dataRow.RowState == DataRowState.Added)
{
//do insert - never goes here, RowState is Detached when it should be Added
}
}
}
Is this the right way to do it? Am I missing something? Thanks in advance.
EDIT: DataGrid binding:
dataGrid1.ItemsSource = cvs.View;