This article assumes that you are familiar with the following topics:
The code in this section declares the shell class to which all DataSetHelper articles add methods and member variables.
1.Start Visual Studio .NET.
2.On the File menu, point to New, and then click Project.
3.In the New Project dialog box, click Visual C# Projects under Project Types, and then click Class Library under Templates.
4.In the Name box, type DataSetHelper.
5.Replace the class code with the following code:
public
class
DataSetHelper
{
public
DataSet ds;
public
DataSetHelper(
ref
DataSet DataSet)
{
ds
=
DataSet;
}
public
DataSetHelper()
{
ds
=
null
;
}
}
You can use the two overloads for the constructor to create an instance of the class with or without a reference to a valid
DataSet. For a class that contains a reference to a valid
DataSet, the
DataTable objects that the methods return are also added automatically to the
DataSet.
NOTE: Add the following to the top of the code window:
using System.Data;
This section contains the code for the SelectDistinct method and the private ColumnEqual helper method.
1.Add the following Private method to the class definition. This method is the same as the method that is used in other DataSetHelper articles. It is used to compare field values (including NULL).
private
bool
ColumnEqual(
object
A,
object
B)
{
//
Compares two values to see if they are equal. Also compares DBNULL.Value.
//
Note: If your DataTable contains object fields, then you must extend this
//
function to handle them in a meaningful way if you intend to group on them.
if
( A
==
DBNull.Value
&&
B
==
DBNull.Value )
//
both are DBNull.Value
return
true
;
if
( A
==
DBNull.Value
||
B
==
DBNull.Value )
//
only one is DBNull.Value
return
false
;
return
( A.Equals(B) );
//
value type standard comparison
}
2.Add the following Public method to the class definition. This method copies unique values of the field that you select into a new DataTable. If the field contains NULL values, a record in the destination table will also contain NULL values.
public DataTable SelectDistinct( string TableName, DataTable SourceTable, string FieldName)
{
DataTable dt = new DataTable(TableName);
dt.Columns.Add(FieldName, SourceTable.Columns[FieldName].DataType);
object LastValue = null ;
foreach (DataRow dr in SourceTable.Select( "" , FieldName))
{
if ( LastValue == null || ! (ColumnEqual(LastValue, dr[FieldName])) )
{
LastValue = dr[FieldName];
dt.Rows.Add( new object []{LastValue});
}
}
if (ds != null )
ds.Tables.Add(dt);
return dt;
}
using System.Data;
DataSet ds;DataSetHelper.DataSetHelper dsHelper;
ds
=
new
DataSet();
dsHelper
=
new
DataSetHelper.DataSetHelper(
ref
ds);
//
Create source table
DataTable dt
=
new
DataTable(
"
Orders
"
);
dt.Columns.Add(
"
EmployeeID
"
, Type.GetType(
"
System.String
"
));
dt.Columns.Add(
"
OrderID
"
, Type.GetType(
"
System.Int32
"
));
dt.Columns.Add(
"
Amount
"
, Type.GetType(
"
System.Decimal
"
));
dt.Rows.Add(
new
object
[] {
"
Sam
"
,
5
,
25.00
});
dt.Rows.Add(
new
object
[] {
"
Tom
"
,
7
,
50.00
});
dt.Rows.Add(
new
object
[] {
"
Sue
"
,
9
,
11.00
});
dt.Rows.Add(
new
Object[] {
"
Tom
"
,
12
,
7.00
});
dt.Rows.Add(
new
Object[] {
"
Sam
"
,
14
,
512.00
});
dt.Rows.Add(
new
Object[] {
"
Sue
"
,
15
,
17.00
});
dt.Rows.Add(
new
Object[] {
"
Sue
"
,
22
,
2.50
});
dt.Rows.Add(
new
object
[] {
"
Tom
"
,
24
,
3.00
});
dt.Rows.Add(
new
object
[] {
"
Tom
"
,
33
,
78.75
});
ds.Tables.Add(dt);
dsHelper.SelectDistinct("DistinctEmployees", ds.Tables["Orders"], "EmployeeID");dataGrid1.SetDataBinding(ds, "DistinctEmployees");
You can only use the function to select a single, distinct field. However, you can extend the functionality to include multiple fields. Alternatively, you can call the CreateGroupByTable, the InsertGroupByInto, and the SelectGroupByInto methods, which use GROUP BY-type functionality, to get the same kind of results.