Hi nedash,
To display the time you need to use GetHour, GetMinute and GetSecond method.
Check this example. Now please take its reference and correct your code.
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 ID="gvEmployees" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField HeaderText="BirthDate">
<ItemTemplate>
<asp:Label ID="lblTime" runat="server" Text='<%#GetTime(Eval("BirthDate").ToString())%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd= new SqlCommand("SELECT EmployeeID, FirstName + LastName AS Name, BirthDate FROM Employees", con))
{
con.Open();
gvEmployees.DataSource = cmd.ExecuteReader();
gvEmployees.DataBind();
con.Close();
}
}
}
public string GetTime(object dateTime)
{
DateTime _dateTime = Convert.ToDateTime(dateTime);
System.Globalization.PersianCalendar pc = new System.Globalization.PersianCalendar();
return string.Format("{0}:{1}:{2}", pc.GetHour(_dateTime), pc.GetMinute(_dateTime), pc.GetSecond(_dateTime));
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT EmployeeID, FirstName + LastName AS Name, BirthDate FROM Employees", con)
con.Open()
gvEmployees.DataSource = cmd.ExecuteReader()
gvEmployees.DataBind()
con.Close()
End Using
End Using
End Sub
Public Function GetTime(ByVal dateTime As Object) As String
Dim _dateTime As DateTime = Convert.ToDateTime(dateTime)
Dim pc As System.Globalization.PersianCalendar = New System.Globalization.PersianCalendar()
Return String.Format("{0}:{1}:{2}", pc.GetHour(_dateTime), pc.GetMinute(_dateTime), pc.GetSecond(_dateTime))
End Function
Screenshot