Display only Date in GridView using C# and VB.Net in ASP.Net

Last Reply 10 days ago By pandeyism

Posted 10 days ago

Dear Sir,

i want to display only date in gridview not date and time both.

i want to display "01-01-1900" only but same are display

"01-01-1900 00:00:00" how to remove time.

Regards,

indradeo

Posted 10 days ago

Hi indradeo,

In which format you want to change you can change in query accordingly to date format will change.

Refer below sample.

HTML

<asp:GridView runat="server" ID="gvDate" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Date" HeaderText="Date" />
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data.SqlClient
Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        SqlConnection con = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand("SELECT CONVERT(VARCHAR,Date,103) FROM tblDateData", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        gvDate.DataSource = dt;
        gvDate.DataBind();
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Dim con As SqlConnection = New SqlConnection(constr)
        Dim cmd As SqlCommand = New SqlCommand("SELECT CONVERT(VARCHAR,Date,103) FROM tblDateData", con)
        Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
        Dim dt As DataTable = New DataTable()
        da.Fill(dt)
        gvDate.DataSource = dt
        gvDate.DataBind()
    End If
End Sub