Sort Datatable randomly
This is not possible to Sort a Datatable directly.
In order to randomize the rows in a DataTable, do the following:
1) Add a random number DataColumn to the DataTable
2) For each row, generate a random number and store it to the new column
3) Create a DataView and sort by the random number DataColumn
Test this by simple ex.
DataTable dt = GetData();
dt.Columns.Add(new DataColumn("RandomNum", Type.GetType("System.Int32")));
Random random = new Random();
for (int i = 0; i < dt.Rows.Count; i++)
{
dt.Rows[i]["RandNum"] = random.Next(1000);
}
DataView dv = new DataView(dt);
dv.Sort = "RandomNum";
In order to randomize the rows in a DataTable, do the following:
1) Add a random number DataColumn to the DataTable
2) For each row, generate a random number and store it to the new column
3) Create a DataView and sort by the random number DataColumn
Test this by simple ex.
DataTable dt = GetData();
dt.Columns.Add(new DataColumn("RandomNum", Type.GetType("System.Int32")));
Random random = new Random();
for (int i = 0; i < dt.Rows.Count; i++)
{
dt.Rows[i]["RandNum"] = random.Next(1000);
}
DataView dv = new DataView(dt);
dv.Sort = "RandomNum";
Comments
"Select Top 10 * from Employees order by newid();"
It works... This gives me a random selection of 10 employees...
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!