Diego Morales Sepulveda Diego Morales Sepulveda - 2 years ago 136
ASP.NET (C#) Question

Date Format to a GridView populated with SQL in ASP.net - NOT boundfield

I'd like to request some help here.

This is the code that creates the gridview.. everything else is happening with C#, I mean that is populated with the C# code..

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="STANDBY_JOURNALS Email.aspx.cs" Inherits="STANDBY_JOURNALS_Email.STANDBY_JOURNALS_Email" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<form id="form1" runat="server">

<asp:Button ID="Button1" runat="server" Text="SQL" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="SEND" OnClick="Button2_Click" />
<br />
<asp:GridView ID="GridView1" runat="server" Font-Size="Smaller">


I need to add Date format to this grid view, but if I try to use BOUNDFIELD, what it does, it creates a column at the beginning of the table, like this:

<asp:GridView ID="GridView1" runat="server" Font-Size="Smaller">
<asp:BoundField DataField="Journal Date" DataFormatString="{0:MMMM d, yyyy}" />

enter image description here

This is my code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace STANDBY_JOURNALS_Email
public partial class STANDBY_JOURNALS_Email : System.Web.UI.Page

SqlConnection vid = new SqlConnection("Data Source=xxxxxxxx; Initial Catalog=xxxxxxxxxxx; User id=xxxxxx; Password=xxxxxxx");

protected void Page_Load(object sender, EventArgs e)


protected void Button1_Click(object sender, EventArgs e)
string sqlquery1 = "SELECT * FROM DBO.STANDBY_JOURNALS_DQMS WHERE [Ledger Grp] IN ('ACTUALS','GRP_FXRATE','CONSEGRP') AND [Source] = 'CON' OR [Source] LIKE 'Q%' ORDER BY [Source], [SystemDateTime],[SystemTime] ASC";

String str = sqlquery1;
SqlCommand xp = new SqlCommand(str, vid);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = xp;
DataSet ds = new DataSet();
da.Fill(ds, "Name");
GridView1.DataSource = ds;
public override void VerifyRenderingInServerForm(Control control)
/* Verifies that the control is rendered */
protected void Button2_Click(object sender, EventArgs e)
using (StringWriter sw = new StringWriter())
using (HtmlTextWriter hw = new HtmlTextWriter(sw))

String EmailFrom = "xxxxxx@xxxxx.co.nz";
String EmailTo = "xxxxxx@xxxxx.co.nz";
String EmailSubject = "this is a subject";
//String EmailBody = "Cuerpo del Mensaje";

StringReader sr = new StringReader(sw.ToString());
MailMessage mm = new MailMessage(EmailFrom, EmailTo);
mm.Subject = EmailSubject;
mm.Body = "Report:<hr />" + sw.ToString(); ;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Port = 25;
smtp.Timeout = 20000;

Anyone can help in how to affect the columns in the table created with the gridview?

thank you very much!!!

Answer Source

set AutoGenerateColumns property of gridview to false(I think this is the only solution you looking for) and add boundfields for all required columns.

or if you are using template fields then There are options to set date format for your template field. No need to add boundfield. like:

<asp:TemplateField ItemStyle-Width = "100px"  HeaderText = "date" 
      SortExpression="date" >
         <asp:Label ID="lblreceived_at" runat="server"
               Text='<%# Eval("date","{0:d}")%>'></asp:Label>

for all options refer : http://www.codedigest.com/Articles/ASPNET/137_How_to_format_DateTime_in_GridView_BoundColumn_and_TemplateColumn.aspx

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download