DataView

Roey

Junior Contributor
Joined
Oct 10, 2002
Messages
238
Location
Canada
I have created a dataview by doing a rowfilter on a dataset. I now want to loop through the dataview and read values in the different columns relevant to the current row. Any ideas on how to do this would be greatly appreciatted.
 
This spins through all the columns in a table of a dataview (right from MSDN documentation):


public static void WriteView(DataView myView)
{
foreach (DataRowView myDRV in myView)
{
for (int i = 0; i < myView.Table.Columns.Count; i++)
Console.Write(myDRV + "\t");
Console.WriteLine();
}
}
 
I have a table with ParentID and ChildID columns in it and using the following code to try to retrieve data from the ChildID column

Dim dvTest As New DataView()
dvTest .Table = dsBillOfMaterial.Tables("BillOfMaterials")
dvTest .RowFilter = "ParentID = '" & strID & " '"

For a = 1 To dvTest .Count
MessagBox.Show(dvTest .Table.Columns("ChildID"))

The ChildID is not being displayed
 
Change it to this:
dim myRowView as dataRowView
For each myRowView in dvTest
MessagBox.Show(myRowView("ChildID"))
Next


I think that should work. Otherwise, use the index of "ChildID" into that table instead of the column name.
 
IMO they messed up when they didn't name that a "DataViewRow", which to me would indicate a row in a DataView. I'm sure they have their reasons:-\
 
Just want to throw in a thanks for the answer that I was looking for...

I couldn't figure out why looping through my typed dataset using the dataview wouldn't sort items.

The DataRowView was it. Thanks!

I was trying:

foreach(LinkCategoryDb.categoryRow category in dataViewCategory.Table.Rows)
{
name.Text = category.name;
}

It needed to be:

foreach(DataRowView category in dataViewCategory)
{
name.Text = (string)category["name"];
}

The confusing part was that it correctly displayed the info using the first method -- it just wasn't sorted...

I'm throwing in some keywords for google. I tried to find the answer for this using:

c# dataview foreach sort
 
Back
Top