Kris Krause .NET Meister

"If it is fast and ugly, they will use it and curse you; if it is slow, they will not use it."
- David Cheriton, The Art of Computer Systems Performance Analysis

Monday, January 31, 2005

DataView vs. DataTable

Yes, I still pull my hair out over DataView versus DataTable every once in a while.

using System;
using System.Collections;
using System.Data;

public class MyTestClass
{
public static void Main()
{
DataTable dt = new DataTable();

try
{
DataColumn col1 = new DataColumn();
col1.DataType = System.Type.GetType("System.Int32");
col1.ColumnName = "EmployeeID";

dt.Columns.Add(col1);

DataColumn col2 = new DataColumn();
col2.DataType = System.Type.GetType("System.String");
col2.ColumnName = "LastName";

dt.Columns.Add(col2);

dt.Rows.Add(new Object[] {1, "Covell"});
dt.Rows.Add(new Object[] {2, "Schaefer"});
dt.Rows.Add(new Object[] {3, "Krause"});
dt.Rows.Add(new Object[] {4, "Meloskie"});
dt.Rows.Add(new Object[] {5, "Vader"});

MyDataView myView = new MyDataView(dt, "EmployeeID < 3");

Console.WriteLine("Filter row count: {0}", myView.CurrentFilterCount);
Console.WriteLine("Row count: {0}", myView.CurrentCount);
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
Console.WriteLine("Done and done.");
Console.ReadLine();
}
}
}

public class MyDataView : DataView
{
public MyDataView(DataTable dt, string filter) : base(dt)
{
this.RowFilter = filter;
}

public int CurrentFilterCount
{
get { return this.Count; }
}

public int CurrentCount
{
get { return this.Table.Rows.Count; }
}
}

Labels:

0 Comments:

Post a Comment

<< Home