*Linq Exist in .Net Framework 3.5++
I've sorting method to sort DataTable to show you today,
-
First is .DefaultView
DataView dv = dt.DefaultView;
dv.Sort = "CustomerName ASC";
-
Second is .Select("leave blank", "Column ASC/DESC")
dt.Select("", "CustomerName");
-
Third id Linq.OrderBy
var SortedData = from a in dt.AsEnumerable()
.Where(a => a.Field("CustomerName") == "Bank of America")
.OrderBy(a => a["CustomerName"])
select a;
The Result
CustomerID | CustomerName |
0 | Adam J |
3 | Bank of America |
1 | Dow jones |
2 | Million Dollar |
Test result in C# here.
//Declare DataTable
DataTable dt = new DataTable("TableName1");
//Add Column
dt.Columns.Add("CustomerID");
dt.Columns.Add("CustomerName");
//Declare Datarow
DataRow dr = null;
dr = dt.NewRow();
dr["CustomerID"] = "0";
dr["CustomerName"] = "Adam J";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["CustomerID"] = "1";
dr["CustomerName"] = "Dow jones";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["CustomerID"] = "2";
dr["CustomerName"] = "Million Dollar";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["CustomerID"] = "3";
dr["CustomerName"] = "Bank of America";
dt.Rows.Add(dr);
DataView dv = dt.DefaultView;
dv.Sort = "CustomerName ASC";
dt.Select("", "CustomerName");
var SortedData = from a in dt.AsEnumerable()
.Where(a => a.Field<string>("CustomerName") == "Bank of America")
.OrderBy(a => a["CustomerName"])
select a;
gv1.DataSource = dt;
gv1.DataBind();
Don't hesitate to contact me zomdev.
No comments:
Post a Comment