create new DataRow, init fields, then set DataRow to UnChanged?

  • Thread starter Thread starter Les Caudle
  • Start date Start date
L

Les Caudle

I'd like to be able to create a new row, init some fields of the row, and then
set the row to DataRowState.Unchanged or dr.AcceptChanges so that when I
sequence thru the rows of the DataTable, this row will not appear to be changed
unless the user actually changes a field.

Unfortunately, you can't AcceptChanges on a new record - and dr.RowState is
readOnly.

Is there some way to accomplish this?

DataTable dt = ds.Tables["Transaction"];
DataRow dr = dt.NewRow();
dr["transDesc"] = "new transaction";
dr["transAmt"] = 0;
dr["transDate"] = DateTime.Now;

// dr.RowState = DataRowState.Unchanged; readonly
// dr.AcceptChanges(); won't work here
dt.Rows.Add(dr);
 
Hi Les,

I did not try it, however does this work?

I typed it here in. So watch typos
DataTable dt = ds.Tables["Transaction"];
DataRow dr = dt.NewRow();
dr["transDesc"] = "new transaction";
dr["transAmt"] = 0;
dr["transDate"] = DateTime.Now;

// dr.RowState = DataRowState.Unchanged; readonly
// dr.AcceptChanges(); won't work here
dt.Rows.Add(dr);

ds.Tables["Transaction"].Rows[ds.Tables["Transaction"].Rows.Count -
1].AcceptChanges();

Cor
 
If you call AcceptChanges on the row after adding it to the DataTable,
you should get the behavior you're looking for.

DataTable dt = ds.Tables["Transaction"];
DataRow dr = dt.NewRow();
dr["transDesc"] = "new transaction";
dr["transAmt"] = 0;
dr["transDate"] = DateTime.Now;

dt.Rows.Add(dr);
dr.AcceptChanges();

I hope this information proves helpful.

David Sceppa
Microsoft
This posting is provided "AS IS" with no warranties,
and confers no rights. You assume all risk for your use.
© 2004 Microsoft Corporation. All rights reserved.
 
Back
Top