PC Review


Reply
Thread Tools Rate Thread

How do I loop through rows in a DataTable?

 
 
TC
Guest
Posts: n/a
 
      7th Oct 2010
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
 
Reply With Quote
 
 
 
 
Tom Shelton
Guest
Posts: n/a
 
      8th Oct 2010
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

--
Tom Shelton


 
Reply With Quote
 
TC
Guest
Posts: n/a
 
      8th Oct 2010
On Oct 8, 1:37*pm, Tom Shelton <tom_shel...@comcast.invalid> wrote:
> 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
>
> --
> Tom Shelton


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
 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      9th Oct 2010
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
news:6858db9c-2e99-428d-9687-(E-Mail Removed)...

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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataTable.Select vs DataTable.rows.Find vs foreach Dave Microsoft C# .NET 1 17th May 2007 09:06 PM
Updating datatable using datatable.rows.find() Lars E Microsoft C# .NET 1 27th Apr 2006 09:28 AM
DataTable.Select() - is it possible to pass rows back to datatable =?Utf-8?B?QW5kcmUgUmFuaWVyaQ==?= Microsoft ADO .NET 5 9th Nov 2005 05:10 PM
Merge DataTable Rows into Second DataTable =?Utf-8?B?TWF0dCBO?= Microsoft ADO .NET 2 12th Apr 2005 04:42 PM
How can I use real SQL on a DataTable? i.e. not array of rows using a filter... as in DataTable.Select Dan V. Microsoft C# .NET 3 1st Jul 2004 03:06 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:39 AM.