在 ASP.net 中填充 SQL 的 GridView 的日期格式 - 非绑定字段

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

这是创建 GridView 的代码,其他一切都是用 C# 发生的,我的意思是用 C# 代码填充:

<%@ 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">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <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">
        </asp:GridView>


    </div>
    </form>
</body>
</html>

我需要将日期格式添加到此 GridView,但如果我尝试使用 BOUNDFIELD,它会在 table 的开头创建一列,如下所示:

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

这是我的代码:

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);
            vid.Open();
            xp.ExecuteNonQuery();
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = xp;
            DataSet ds = new DataSet();
            da.Fill(ds, "Name");
            GridView1.DataSource = ds;
            GridView1.DataBind();
            vid.Close();
        }
        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";



                    GridView1.RenderControl(hw);
                    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.Host = "XXXXXXXXXXXXXX";
                    smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                    smtp.Port = 25;
                    smtp.Timeout = 20000;
                    smtp.Send(mm);
                }
            }
        }
    }
}

任何人都可以帮助了解如何影响使用 GridView 创建的 table 中的列?

将 GridView 的 AutoGenerateColumns 属性 设置为 false(我认为这是您正在寻找的唯一解决方案)并为所有必需的列添加 BoundFields。

或者,如果您使用的是 TemplateFields,则可以选择为 TemplateField 设置日期格式。无需添加 BoundField 如:

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

所有选项请参考: http://www.codedigest.com/Articles/ASPNET/137_How_to_format_DateTime_in_GridView_BoundColumn_and_TemplateColumn.aspx

在 BoudField 中,试试这个格式:

DataFormatString="{0:dd/MM/yyyy}"

输出:

01/06/2017 00:00:00

最简单的方法就是在查询中使用 SQL MID()

SELECT MID(Journal_Date,1,8) AS JD FROM DBO.STANDBY_JOURNALS_DQMS

它会给你

01/06/17

如果您在 SQL 查询中添加以下 Convert 函数,您的目的就会达到。例如:

Select CONVERT(varchar,YOUR_DATE_COLUMN,107) as JournalDate, * From YOUR_TABLE_NAME