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

Last Reply one month ago By pandeyism

Posted one month 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

.aspx
<%@ Page Language="C#" AutoEventWireup="true" Codefile="GeneReport.aspx.cs" Inherits="ContractCloser1.GeneReport" %>


<html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">

     

            <style type="text/css">
                .auto-style1 {
                    font-weight: bold;
                    text-decoration: underline;
                }
                .auto-style2 {
                    text-decoration: underline;
                }
            </style>
 <span class="style1">

     

<font color=White size=5>
    <div style="margin-left:5px; width: 564px; height: 400px;"
        <b><font face="Arial" size="6"><br class="auto-style2" /><br class="auto-style2" />
 <span class="auto-style1">
    <font color=black size=6>Generate Department LOA Details</font></span></font></font></h1>    



   

      

   
                           
<form id="form1" runat="server">  

   

    <span class="style1">
    <font face="Arial" size="4">  
    <div style="margin-left: 0px; width: 566px; height: 195px;" class="auto-style1">  
      
        <div class="auto-style2">
      
        <br /> 
        Select Department  
        <asp:DropDownList ID="DropDownList1" runat="server">  
        </asp:DropDownList>  
       
        <br />  
        <br />  
        <asp:Button ID="Button1" runat="server" Text="GetData" OnClick="Button1_Click1" Style="height: 26px" />  
        <br />  
        <br />  
        </div>
        <asp:GridView ID="GridView1" runat="server" BackColor="black" BorderColor="#999999"  
            BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" Width="561px" style="text-align: center">  
            <AlternatingRowStyle BackColor="" />  
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />  
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />  
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />  
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />  
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />  
            <SortedAscendingCellStyle BackColor="#F1F1F1" />  
            <SortedAscendingHeaderStyle BackColor="#0000A9" />  
            <SortedDescendingCellStyle BackColor="#CAC9C9" />  
            <SortedDescendingHeaderStyle BackColor="#000065" />  
            

              
        </asp:GridView>  
    </div>  
    </font></span></font>  
    </form>  
    </form>
</body>
</html>

.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;    
     
namespace ContractCloser1
{
    public partial class GeneReport : System.Web.UI.Page
    {
        string str = @"Data Source=(localdb)\Projects;Initial Catalog=Emp;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False";


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTextBoxvalues();
            }
        }
             private void BindTextBoxvalues()
            {
            SqlConnection con = new SqlConnection(str);
            string com = "Select * from dept_mast";
            SqlDataAdapter adpt = new SqlDataAdapter(com, con);
            DataTable dt = new DataTable();
            adpt.Fill(dt);
            DropDownList1.DataSource = dt;
            DropDownList1.DataBind();
            DropDownList1.DataTextField = "dept_desc";
            DropDownList1.DataValueField = "dept_code";
            DropDownList1.DataBind();    
            }
    
        protected void Button1_Click1(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(str);
            SqlCommand cmd = new SqlCommand("select * from loa_closer where dept_code = '" + DropDownList1.SelectedValue + "'", con);
           
            SqlDataAdapter adpt = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adpt.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
          
        }
    }
}

 

You are viewing reply posted by: pandeyism one month ago.
Posted one month ago

Hi indradeo,

Refer below sample.

HTML

<asp:GridView runat="server" ID="gvDate" AutoGenerateColumns="true" OnRowDataBound="gvDate_RowDataBound">
</asp:GridView>

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("DateTime");
        dt.Rows.Add(DateTime.Now.AddDays(-1));
        dt.Rows.Add(DateTime.Now.AddDays(-2));
        dt.Rows.Add(DateTime.Now.AddDays(-3));
        dt.Rows.Add(DateTime.Now);
        gvDate.DataSource = dt;
        gvDate.DataBind();
    }
}

protected void gvDate_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DateTime date = Convert.ToDateTime(e.Row.Cells[0].Text);
        e.Row.Cells[0].Text = date.ToString("dd-MM-yyyy");
    }
}

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.Add("DateTime")
        dt.Rows.Add(DateTime.Now.AddDays(-1))
        dt.Rows.Add(DateTime.Now.AddDays(-2))
        dt.Rows.Add(DateTime.Now.AddDays(-3))
        dt.Rows.Add(DateTime.Now)
        gvDate.DataSource = dt
        gvDate.DataBind()
    End If
End Sub

Protected Sub gvDate_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim date1 As DateTime = Convert.ToDateTime(e.Row.Cells(0).Text)
        e.Row.Cells(0).Text = date1.ToString("dd-MM-yyyy")
    End If
End Sub

Output

DateTime
18-08-2019
17-08-2019
16-08-2019
19-08-2019

For more details refer below article.

Format DateTime column (field) in ASP.Net GridView with AutoGenerateColumns True