Iterating DataViews

Published Sat, May 1 2004 9:58 | William

One of my favorite objects in ADO.NET is

<A TARGET='_blank' HREF='http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataDataViewClassTopic.asp'>DataView</A> Class. Basically, ADO.NET uses a Database metaphor where the <A TARGET='_blank' HREF='http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataDataSetClassTopic.asp'>DataSet</A> object corresponds to a "database", the <A TARGET='_blank' HREF='http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatatableclasstopic.asp'>DataTable</A> class corresponds to a table in that database and the dataview corresponds to a "view" object. It's a pretty good metaphor, but it's not perfect. One of the main differences is that a DataView is only based on one table where in a database, it's common practice to create a view on multiple joined tables. With that distinction, the metaphor holds. As such, each tuple in a database table corresponds to a <A TARGET='_blank' HREF='http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatarowclasstopic.asp'>DataRow </A> object. So iterating throught a datatable entails walking through its <A TARGET='_blank' HREF='http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatatableclassrowstopic.asp'>Rows</A> collection. So a common assumption is that a DataView has a Rows collection as well. After all, there's really not a difference in how you deal with a Row in a database table and a database view. There's a slight distinction in ADO.NET. A DataView is composed of <A TARGET='_blank' HREF='http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatarowviewclasstopic.asp'>DataRowView</A> objects. In a datatable, you get the number of tuples by using DataTable.Rows.Count property. In a Dataview, you just use the DataView's <A TARGET='_blank' HREF='http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadataviewclasscounttopic.asp'>.Count</A> property.

Anyway, enough background. If you want to iterate through a DataView, I show you three different methods in both VB.NET and C#. I use two different constructors just to show you they are equal (probably doesn't fit in with the rest of the discussion, but I figured I'd mention it). The comments should explain everything, but if you have any questions, my email is provided on my profile and by all means drop me a line:

VB.NET

Private

Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim dt As New DataTable

da.Fill(dt)

Debug.WriteLine(dt.Rows.Count.ToString)

'Create 2 new DataViews, both yeild the same result

'Note that there's a third constructor which allows you

'to specify a RowFilter, Sort, and RowStateFilter

Dim dv As DataView = dt.DefaultView

Dim dv2 As DataView = New DataView(dt)

Debug.Assert(dv.Count = dv2.Count)

'Let walk through the first one using IEnumerator

Dim iterator As IEnumerator = dv.GetEnumerator

Dim drv As System.Data.DataRowView

Dim i As Integer = 0

While iterator.MoveNext

drv =

CType(iterator.Current, System.Data.DataRowView)

i += 1

End While

Dim x As Integer = 0

For Each drv2 As System.Data.DataRowView In dv2

x += 1

Next

Dim a As Integer = 0

For z As Integer = 0 To dv.Count - 1

a += 1

Next

Debug.Assert(i = dt.Rows.Count)

Debug.Assert(x = dt.Rows.Count)

Debug.Assert(a = dt.Rows.Count)

End

Sub

 

C#

private

void Form1_Load(object sender, System.EventArgs e)

{

DataTable dt =

new DataTable();

da.Fill(dt);

Debug.WriteLine(dt.Rows.Count.ToString());

//Create 2 new DataViews, both yeild the same result

//Note that there's a third constructor which allows you

//to specify a RowFilter, Sort, and RowStateFilter

DataView dv = dt.DefaultView;

DataView dv2 =

new DataView(dt);

//Assertion does not fail so they are the same.

Debug.Assert(dv.Count == dv2.Count);

IEnumerator iterator = dv.GetEnumerator();

DataRowView drv;

System.Int32 i= 0;

while(iterator.MoveNext())

{

drv = (DataRowView)iterator.Current;

i++;

}

System.Int32 x = 0;

foreach(DataRowView drv2 in dv)

{

x++;

}

System.Int32 a = 0;

//Remember this is C#, so we don't want to subtract the 1 from Count

for(System.Int32 z = 0; z < dv.Count; z++)

{

a++;

}

Debug.Assert(i == dt.Rows.Count);

Debug.Assert(x == dt.Rows.Count);

Debug.Assert(a == dt.Rows.Count);

}

Comments

# TrackBack said on May 6, 2004 6:41 AM:

Search

This Blog

Tags

Community

Archives

News

My other sites

Cool Stuff

Book Stuff

Security

ORM

Data Access

Funny Stuff

Compact Framework Stuff

Web Casts

My KnowledgeBase Articles

My MVP Profile

Design Patterns

Performance

Debugging

Remoting

My Fellow Authors

My Books

LINQ

Misc

Speech

Syndication

Email Notifications