Added Row in DataGrid Not Correctly Written To DataSet

G

Guest

Hi Folks

I have created a DataGrid that I populate through a DataView. The DataView
itself represents only parts of the columns and only parts of the dataSet's
Rows.

When the User is adding a new Row in the dataGrid, I fill all fields with
default values using the ListChanged event of the dataView.

This works fine. Now the User can himself doing some modifications to the
fields.

When he now leaves the Row, not the actual values but the older default
values are stored to the DataSet. All User made modifications are lost.

Thanks for your effort
Chris
 
B

Bart Mermuys

Hi,

Hi Folks

I have created a DataGrid that I populate through a DataView. The DataView
itself represents only parts of the columns and only parts of the
dataSet's
Rows.

When the User is adding a new Row in the dataGrid, I fill all fields with
default values using the ListChanged event of the dataView.

ListChanged is called twice, once when DataTable.NewRow() is called and once
when the new row is added to the DataTable.Rows collections. So check for
the first state where RowState = Detached :

private void DefaultView_ListChanged(object sender, ListChangedEventArgs e)
{
if ( e.ListChangedType == ListChangedType.ItemAdded &&
dataView[e.NewIndex].Row.RowState == DataRowState.Detached )
{
// set defaults
}
}


Or maybe better, use the DefaultValue property of the DataColumn(s) in the
DataTable.


HTH,
Greetings
 
A

Alex Passos

Is the DataView.AllowEdit = true?

This is straight from MSDN:

private void EditRow(DataView dv)
{
dv.AllowEdit = true;
dv[0].BeginEdit();
dv[0]["FirstName"] = "Mary";
dv[0]["LastName"] = "Jones";
dv[0].EndEdit();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top