How to create a datatable object from a datarow [].

  • Thread starter Thread starter jensen bredal
  • Start date Start date
J

jensen bredal

I have an array of datarow . Is there a performance efficient way of
deriving a datatable oject
containing only the rows in my array. ?

I want to avoid uding foreach .

Many thanks

JB
 
where did you get the array of datarows?? the only way i know of is
looping through them and using dt.Rows.Add(...); if there's a better
way, i'd also like to know...
 
if there's a better
way, i'd also like to know...

Here is the code:

DataSet ds=GetDataSet();


DataTable dt=ds.Tables[0];

int i=5, j=11;

if(dt.Rows.Count-i>0)

{

DataRow[] selectedRows=new DataRow[(j-i)];

for(int index=0; index<selectedRows.Length; index++)

{

selectedRows[index]=dt.Rows[i+index];

}


return selectedRows;
 
Hi,

What about using a DataView ?

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



jensen bredal said:
if there's a better
way, i'd also like to know...

Here is the code:

DataSet ds=GetDataSet();


DataTable dt=ds.Tables[0];

int i=5, j=11;

if(dt.Rows.Count-i>0)

{

DataRow[] selectedRows=new DataRow[(j-i)];

for(int index=0; index<selectedRows.Length; index++)

{

selectedRows[index]=dt.Rows[i+index];

}


return selectedRows;
 
Basically, it would be along the lines of:

DataSet ds=GetDataSet();
DataTable dt=ds.Tables[0];
DataView dv = new DataView(dt, "filter expression", "sort expression",
DataViewRowState.CurrentRows)
return dv;

where "filter expression" would be exactly as you'd write it in a WHERE
clause, and sort expression just as you'd write it in a ORDER BY.

Presumably, you have a better way of expressing the subset you want to
filter on, than "rows 5 thru 11"/
--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
Presumably, you have a better way of expressing the subset you want to
filter on, than "rows 5 thru 11"/
--

That is the problem. I see no obvious way of doing this through a sort
expression.

5 through 11 is in fact almost what i'm trying to do. I have a datatable
with n rows.

each time i only wnat to subtract a subset and the nex subset the next time
and so on.
I see no obvious way to this with sql.
Do you?
JB
 
Back
Top