How do I loop through rows in a DataTable?

  • Thread starter Thread starter TC
  • Start date Start date
T

TC

I'm working on an application which must manipulate data in a DataSet.
Very often, I must loop through the rows in a DataTable. To do that,
I've been writing code that looks like this:

For Each ElementRow As DataRow In ElementTable.Select("TRUE", "",
DataViewRowState.CurrentRows)
....

That works, but I suspect it isn't the best way. After all, it seems
inappropriate that I should have to provide values for
FilterExpression and Sort when I don't want a filter and don't care
about the sort order. Have I overlooked a better way to loop through
the rows in a DataTable?

-TC
 
TC pretended :
I'm working on an application which must manipulate data in a DataSet.
Very often, I must loop through the rows in a DataTable. To do that,
I've been writing code that looks like this:

For Each ElementRow As DataRow In ElementTable.Select("TRUE", "",
DataViewRowState.CurrentRows)
...

That works, but I suspect it isn't the best way. After all, it seems
inappropriate that I should have to provide values for
FilterExpression and Sort when I don't want a filter and don't care
about the sort order. Have I overlooked a better way to loop through
the rows in a DataTable?

-TC

Is there now Rows property on your datatable? I never use typed
datasets (and rarely datasets at all - prefering to use domain objects
instead) - so, I honestly don't know... But, with a regular datatable,
you can do:

For Each r As DataRow in MyTable.Rows
...
Next

There is also the Linq-DataSet extensions if you want :)
 
TC pretended :





Is there now Rows property on your datatable?  I never use typed
datasets (and rarely datasets at all - prefering to use domain objects
instead) - so, I honestly don't know... But, with a regular datatable,
you can do:

For Each r As DataRow in MyTable.Rows
   ...
Next

There is also the Linq-DataSet extensions if you want :)

Tom,

Yes, there is a Rows property, but it includes deleted rows. Frankly,
I find ADO.NET to be very confusing in this regard. When you delete a
row, you don't actually delete it. It stays in the DataTable, but you
get an error if you try to use it. In order to work with the current
state of the DataTable -- as I assume most people want to do 99.9% of
the time -- you must somehow filter out deleted rows. One way to do
that is to specify the row state (as with DataViewRowState.CurrentRows
in my example), but I can find no way to specify the row state without
also specifying a filter expression ("TRUE" in my example) and a sort
order ("" in my example).

Thanks for the suggestion about Linq. If necessary, I'll look into it.
However, I don't want to adopt a new technology until I confirm that
1) there is a problem with the old technology; and 2) the new
technology is better. Do you use Linq, and can you recommend it?

-TC
 
Ever tried the defaultview (is a standard property in a datatable from the
type dataview)
or simple a dataview

dim dv as new DataView(Elements.Table)
dv.RowFilter = "WhateverColumname = FilterElement"

for each drv in dv
drv("Columname") ..................................
next

For each ElementsDataRowView as DataRowView in

Success

Cor

"TC" wrote in message

I'm working on an application which must manipulate data in a DataSet.
Very often, I must loop through the rows in a DataTable. To do that,
I've been writing code that looks like this:

For Each ElementRow As DataRow In ElementTable.Select("TRUE", "",
DataViewRowState.CurrentRows)
....

That works, but I suspect it isn't the best way. After all, it seems
inappropriate that I should have to provide values for
FilterExpression and Sort when I don't want a filter and don't care
about the sort order. Have I overlooked a better way to loop through
the rows in a DataTable?

-TC
 
Back
Top