Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a dataset with a column "KeyName". I need to filter my for each loop on this if I can.

For Each oDataRow In oDataSet.Tables(0).Rows
   'code
Next

Something like this...

e.g.

For Each oDataRow In oDataSet.Tables(0).Rows (where "KeyName = 'SomeValue'")
   'code
Next

The reason I need to do this is THIS For Each loop is nested inside another one where I am looping though each "KeyName" in an XML Document.

Looks like this.

For Each lo_node In lo_nodeSet.SelectNodes("attachments")
   lo_attachments = GetDataElement(lo_node, "attachments")
   ls_name = lo_attachments.GetAttribute("KeyName")
   'some code
   For Each oDataRow In oDataSet.Tables(0).Rows (where KeyName = ls_name)
       'BUNCH of code
   Next 
   'more code
Next

Hope that makes sense. Thanks

Posted

My post count reflects my experience with .Net. :D This is my first attempt at a .net app using a database, so my knowledge on these datasets...etc. is kind of limited.

 

But what you say sounds good. After messing with it a bit. I came up with this.

oDataView = oDataTable.DefaultView
For Each lo_node In lo_nodeSet.SelectNodes("attachments")
   lo_attachments = GetDataElement(lo_node, "attachments")
   ls_name = lo_attachments.GetAttribute("KeyName")
   oDataView.RowFileter = "name = '" & ls_name & "'"
   'some code
   For Each oDataRow In oDataView.Table.Rows
       'some code
   Next 
   'more code
Next

And works great. Thanks!

 

These .net data objects are starting to click a little bit now.

Posted

This shouldn't work as expected.

You are ignoring the view with oDataView.Table.Rows.

You have to use

oDataView = oDataTable.DefaultView
For Each lo_node In lo_nodeSet.SelectNodes("attachments")
   lo_attachments = GetDataElement(lo_node, "attachments")
   ls_name = lo_attachments.GetAttribute("KeyName")
   oDataView.RowFileter = "name = '" & ls_name & "'"
   'some code
   For Each oDataRowView In oDataView
       'some code
   Next 
   'more code
Next 

  • 2 weeks later...
Posted

I don't see why you say "This shouldn't work"...? :confused:

 

A Dataview object has a Table (DataTable) object, which has a Rows (DataRowCollection) object... It's a simple loop through the rows collection.

 

And it works perfectly.

Posted

The Table in DataView is the unfiltered Table.

So you are not looping through the filtered values.

 

Easier for your purpose would be the DataTable.Select() funktion.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...