Select Distinct From DataSet


This step-by-step article illustrates the equivalent to select distinct values from one or more column of a DataSet or DataTable
Example :

NOTE: Add the following to the top of the code window:
 

using System.Data;

//Add the following constructor code :

System.Data.DataSet ds = new System.Data.DataSet();

// Create source table

System.Data.DataTable dt = new System.Data.DataTable("MyTableName");
dt.Columns.Add("ColumnA", Type.GetType("System.Int32"));
dt.Rows.Add(1);
dt.Rows.Add(2);
dt.Rows.Add(3);
dt.Rows.Add(1);
Ds.Tables.Add(dt);
System.Data.DataView Dv = Ds.Tables["MyTableName"].DefaultView;
System.Data.DataTable DtD = Dv.ToTable(true, "ColumnA");
//DtD contains only Distinct values (1,2 and 3)

The command Dv.ToTable(true, .. help you to Select Distinct from Ds DataSet the column : "ColumnA"

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites