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

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);
 
C

Cor Ligthert

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
 
D

David Sceppa

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.
 

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