Hi zeeshanpas,
Convert the Date column to DateTime using Convert.ToDateTime method inside the OrderByDescending method.
Please refer below example.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:GridView runat="server" ID="gvDetails" AutoGenerateColumns="true">
</asp:GridView>
Namespace
C#
using System.Data;
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("EmployeeID"),
new DataColumn("LastName"),
new DataColumn("FirstName"),
new DataColumn("Title"),
new DataColumn("BirthDate"),
new DataColumn("HireDate"),
new DataColumn("address")});
dt.Rows.Add("1", "Davolio", "Nancy", "Sales Representative", "1948-12-08 00:00:00.000", "1992-05-01 00:00:00.000", "507 - 20th Ave. E. Apt. 2A");
dt.Rows.Add("2", "Fuller", "Andrew", "Vice President, Sales", "1952-02-19 00:00:00.000", "1992-08-14 00:00:00.000", "908 W. Capital Way");
dt.Rows.Add("3", "Leverling", "Janet", "Sales Representative", "1963-08-30 00:00:00.000", "1992-04-01 00:00:00.000", "722 Moss Bay Blvd.");
dt.Rows.Add("4", "Peacock", "Margaret", "Sales Representative", "1937-09-19 00:00:00.000", "1993-05-03 00:00:00.000", "4110 Old Redmond Rd.");
dt.Rows.Add("5", "Buchanan", "Steven", "Sales Manager", "1955-03-04 00:00:00.000", "1993-10-17 00:00:00.000", "14 Garrett Hill");
dt.Rows.Add("6", "Suyama", "Michael", "Sales Representative", "1963-07-02 00:00:00.000", "1993-10-17 00:00:00.000", "Coventry House Miner Rd.");
dt.Rows.Add("7", "King", "Robert", "Sales Representative", "1960-05-29 00:00:00.000", "1994-01-02 00:00:00.000", "Edgeham Hollow Winchester Way");
dt.Rows.Add("8", "Callahan", "Laura", "Inside Sales Coordinator", "1958-01-09 00:00:00.000", "1994-03-05 00:00:00.000", "4726 - 11th Ave. N.E.");
dt.Rows.Add("9", "Dodsworth", "Anne", "Sales Representative", "1966-01-27 00:00:00.000", "1994-11-15 00:00:00.000", "7 Houndstooth Rd.");
var obj = (from DataRow row in dt.Rows
select new
{
EmployeeID = row["EmployeeID"] == DBNull.Value ? "" : row["EmployeeID"].ToString(),
LastName = row["LastName"] == DBNull.Value ? "" : row["LastName"].ToString(),
FirstName = row["FirstName"] == DBNull.Value ? "" : row["FirstName"].ToString(),
Title = row["Title"] == DBNull.Value ? "" : row["Title"].ToString(),
BirthDate = row["BirthDate"] == DBNull.Value ? "" : Convert.ToDateTime(row["BirthDate"]).ToString("dd-MM-yyyy"),
HireDate = row["HireDate"] == DBNull.Value ? "" : row["HireDate"].ToString(),
address = row["address"] == DBNull.Value ? "" : row["address"].ToString(),
}).OrderByDescending(a => Convert.ToDateTime(a.BirthDate)).ToList();
gvDetails.DataSource = obj;
gvDetails.DataBind();
}
}