One way to sort a DataTable is to set "Sort" property of DefaultView of a DataTable and then convert the sorted DataView to DataTable by .ToTable() method.
DataView.Sort Property
http://msdn.microsoft.com/en-us/library/system.data.dataview.sort.aspx
DataView.ToTable Method
http://msdn.microsoft.com/en-us/library/a8ycds2f.aspx
Example:
DataTable myDT = new DataTable();
Random rndm = new Random();
myDT.Columns.Add("iValue", typeof(System.Int32) );
for (int i = 0; i < 10; i++)
{
DataRow myDR = myDT.NewRow();
myDR["iValue"] = rndm.Next();
myDT.Rows.Add(myDR);
}
string sortExp = "iValue";
string sortOrder = "DESC";
myDT.DefaultView.Sort = sortExp + " " + sortOrder;
myDT = myDT.DefaultView.ToTable();
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment