Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataView: loop through columns

Status
Not open for further replies.

ispeaker

Programmer
Dec 29, 2005
15
US
OK. After two hours on Google I can not find an example to loop through a DataView the way I want to. Maybe its got something to do with Monday morning. Well, here is what I what to do:

For each row in dataview
For each col in dataview
debug.writeline(the current column)
Next
Next

Any suggestions?
 
Without testing it I guess it would be something like:
Code:
        For Each dr As DataRow In dv.Table.Rows
            For i = 1 To dv.Table.Columns.Count
                Console.WriteLine(dr.Item(i))
            Next
            i = 1
        Next


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Thank you, but I must ask this question: Why is it that when i type "dv." the only option intellisense gives me is "GetType"? Why don't I see "Table"?
 
Have you declared dv as DataView - or better still replace dv with the name of your DataView.


Hope this helps.

[vampire][bat]
 
Here is the exact code:

Code:
Dim dvPartsPricing As DataView
dvPartsPricing = dsPartsPricing.Tables(0).DefaultView
dvPartsPricing.Sort = "CatMainSubGID, Length"
 
To correctly navigate DataViews you need to use the DatarowView.
Code:
Dim dv As DataView = dt.DefaultView
dv.Sort = "field1,field2"

For Each rv As DataRowView In dv
    'Get the Datarow
    Dim r As DataRow = rv.Row
    For i As Integer = 0 To r.ItemArray.Length - 1
       Debug.WriteLine(r(i))
    Next
Next



Sweep
...if it works dont f*** with it
curse.gif
 
SqueakinSweep, what makes this the "correct" way?
 
ca8msm,

No matter what I tried, I could not get anything but "GetType" to show in intellisense. BUT, this morning, after re-boot the computer, I get the full list of intellisense properties and methods. Mmmmm....... Maybe a problem with my Visual Studio 2003.

Anyways, thanks again for the help, and what do you think of SqueakinSweep suggestions that there is a "correct" way to do this? I like your solution; its simple and straight-forward.
 
I would imagine that Sweep is correct. It makes sense that each row that you loop through should be a DataRowView rather than a DataRow.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
It is the correct way, because if you use dv.table, you are referencing the datatable, and you lose the sort order which was the whole point of the view in the first place.

The code I provided is the for each mathod of skipping through a dataview


Sweep
...if it works dont f*** with it
curse.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top