// This method copies the unique value of the selected field to a new able. If the field contains null values, the records in the target table 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;
}
Static Bool Columnequal ( Object A, Object B)
{
If ( = Dbnull. Value && B = Dbnull. value) // Both are dbnull. Value
Return True ;
If ( = Dbnull. Value | B = Dbnull. value) // Only one is dbnull. Value
Return False ;
Return (A. Equals (B )); // Value Type Standard Comparison
}