DataTable.LoadDataRow - can you improve on this?

  • Thread starter Thread starter Jim Lawton
  • Start date Start date
J

Jim Lawton

DataTable is dt, filled already and with keys defined, uid, seq etc, suitable
variables ...

object[] newRow = new object[5];

newRow[0] = uid;
newRow[1] = seq;
newRow[2] = text;
newRow[3] = type;
newRow[4] = data;

dt.LoadDataRow(newRow,false);

DataRow newRow = dt.NewRow();

newRow["uid"] = uid;
newRow["seq"] = seq;

any suggestions for achieving the required effect - loading data without
hardwiring the column positions?

Cheers
Jim
 
Hi Jim,

if you use your last suggestion:
DataRow newRow = dt.NewRow();
newRow["uid"] = uid;
newRow["seq"] = seq;

you can then use

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

to add your row to the datatable and mark it as Unchanged, i e loading it.

To improve performance I would suggest you use the DataColumn objects of the
datatable when indexing the row instead of their names, i e
newRow[uidColumn] instead of newRow["uid"]. And if you add more than one row
at the time, don't forget to call BeginLoadData() before and EndLoadData()
after the loading to further improve performance.

Hope this helps,

/Markus




Jim Lawton said:
DataTable is dt, filled already and with keys defined, uid, seq etc, suitable
variables ...

object[] newRow = new object[5];

newRow[0] = uid;
newRow[1] = seq;
newRow[2] = text;
newRow[3] = type;
newRow[4] = data;

dt.LoadDataRow(newRow,false);
course I could use an enumeration for readability but that wouldn't remove
the underlying problem
DataRow newRow = dt.NewRow();

newRow["uid"] = uid;
newRow["seq"] = seq;
..LoadDataRow, which allows me to load that data ...
 
Hi Jim,

if you use your last suggestion:
DataRow newRow = dt.NewRow();
newRow["uid"] = uid;
newRow["seq"] = seq;

you can then use

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

to add your row to the datatable and mark it as Unchanged, i e loading it.

To improve performance I would suggest you use the DataColumn objects of the
datatable when indexing the row instead of their names, i e
newRow[uidColumn] instead of newRow["uid"]. And if you add more than one row
at the time, don't forget to call BeginLoadData() before and EndLoadData()
after the loading to further improve performance.

Hope this helps,
Thanks Markus - that is helpful, in relation to performance.
I should however have said that the row might be preexisting, so I need the
insert/update functionality of LoadDataRow ....

Jim
 
OK, but using the ItemArray property of the DataRow you can do it like this
then:

DataRow newRow = dt.NewRow();
newRow["uid"] = uid;
newRow["seq"] = seq;
dt.LoadDataRow(newRow.ItemArray, false);

My comments on performance still holds and could be added to the code above,
if necessary.

Markus

Jim Lawton said:
Hi Jim,

if you use your last suggestion:
DataRow newRow = dt.NewRow();
newRow["uid"] = uid;
newRow["seq"] = seq;

you can then use

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

to add your row to the datatable and mark it as Unchanged, i e loading it.

To improve performance I would suggest you use the DataColumn objects of the
datatable when indexing the row instead of their names, i e
newRow[uidColumn] instead of newRow["uid"]. And if you add more than one row
at the time, don't forget to call BeginLoadData() before and EndLoadData()
after the loading to further improve performance.

Hope this helps,
Thanks Markus - that is helpful, in relation to performance.
I should however have said that the row might be preexisting, so I need the
insert/update functionality of LoadDataRow ....

Jim
 
OK, but using the ItemArray property of the DataRow you can do it like this
then:

DataRow newRow = dt.NewRow();
newRow["uid"] = uid;
newRow["seq"] = seq;
dt.LoadDataRow(newRow.ItemArray, false);

aha .... newRow.ItemArray ...

that's the baby, I wasn't concentrating :-(
thanks Markus
 
Back
Top