Hii Lingers,
Kindly refer below sample.
Darabase
HTML
<asp:GridView ID="avDetails" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:TemplateField HeaderText="PROD CODE" SortExpression="Id">
<ItemTemplate>
<%# Eval("Id")%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="deliverydate" HeaderText="RECEIVED" SortExpression="deliverydate" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Globalization;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Globalization
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
avDetails.DataSource = BindUserDetails1("");
avDetails.DataBind();
}
}
private DataTable BindUserDetails1(string sortExpession)
{
string conn = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conn))
{
using (SqlCommand cmd = new SqlCommand("SELECT ID, DeliveryDate FROM Datetable", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string _date = e.Row.Cells[1].Text;
DateTime dt;
if (DateTime.TryParse(_date, out dt))
{
e.Row.Cells[1].Text = dt.ToString("dd/MM/yyyy");
}
else if (DateTime.TryParse(_date, CultureInfo.CreateSpecificCulture("en-US"), DateTimeStyles.None, out dt))
{
e.Row.Cells[1].Text = dt.ToString("dd/MM/yyyy");
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
avDetails.DataSource = BindUserDetails1("")
avDetails.DataBind()
End If
End Sub
Private Function BindUserDetails1(ByVal sortExpession As String) As DataTable
Dim conn As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conn)
Using cmd As SqlCommand = New SqlCommand("SELECT ID, DeliveryDate FROM Datetable", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Using
End Function
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim _date As String = e.Row.Cells(1).Text
Dim dt As DateTime
If DateTime.TryParse(_date, dt) Then
e.Row.Cells(1).Text = dt.ToString("dd/MM/yyyy")
ElseIf DateTime.TryParse(_date, CultureInfo.CreateSpecificCulture("en-US"), DateTimeStyles.None, dt) Then
e.Row.Cells(1).Text = dt.ToString("dd/MM/yyyy")
End If
End If
End Sub
Screenshot