Hi zeeshanpas,
Use DateTime.ParseExact 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="false">
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="ID" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:BoundField DataField="BirthDate" HeaderText="Birth Date" />
</Columns>
</asp:GridView>
Namespace
C#
using System.Data;
using System.Globalization;
VB.Net
Imports System.Data
Imports System.Globalization
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 ? "" : Convert.ToDateTime(row["HireDate"]).ToString("dd-MM-yyyy"),
address = row["address"] == DBNull.Value ? "" : row["address"].ToString(),
}).OrderByDescending(a => DateTime.ParseExact(a.BirthDate, "dd-MM-yyyy", CultureInfo.InvariantCulture)).ToList();
gvDetails.DataSource = obj;
gvDetails.DataBind();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = 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.")
Dim obj = (From row In dt.Rows
Select New With {
.EmployeeID = If(row("EmployeeID") = DBNull.Value, "", row("EmployeeID").ToString()),
.FirstName = If(row("FirstName") = DBNull.Value, "", row("FirstName").ToString()),
.Title = If(row("Title") = DBNull.Value, "", row("Title").ToString()),
.BirthDate = If(row("BirthDate") = DBNull.Value, "", Convert.ToDateTime(row("BirthDate")).ToString("dd-MM-yyyy")),
.HireDate = If(row("HireDate") = DBNull.Value, "", Convert.ToDateTime(row("HireDate")).ToString("dd-MM-yyyy")),
.address = If(row("address") = DBNull.Value, "", row("address").ToString())
}).OrderByDescending(Function(a) DateTime.ParseExact(a.BirthDate, "dd-MM-yyyy", CultureInfo.InvariantCulture)).ToList()
gvDetails.DataSource = obj
gvDetails.DataBind()
End If
End Sub
Screenshot