DataTable class corresponds to a table in that database and the dataview corresponds to a "view" object.">
KnowDotNet Visual Organizer

Walking through a DataView

by William Ryan
Print this Article Discuss in Forums

One of my favorite objects in ADO.NET is DataView Class.  Basically, ADO.NET uses a Database metaphor where the DataSet object corresponds to a "database", the DataTable 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 DataRow object.  So iterating throught a datatable entails walking through its Rows 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 DataRowView 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 .Count 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++)<BR>       {
       a++;
   }

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

}

Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing