【问题标题】:Date Format to a GridView populated with SQL in ASP.net - NOT boundfield在 ASP.net 中填充 SQL 的 GridView 的日期格式 - 不是绑定字段
【发布时间】:2017-07-26 03:54:55
【问题描述】:

这是创建 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,它会在表格的开头创建一个列,如下所示:

<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 创建的表中的列?

【问题讨论】:

  • 因为你使用的是boundfield。改为编辑模板字段的格式

标签: c# asp.net gridview date-format


【解决方案1】:

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

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

<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

【讨论】:

  • 你能帮助我如何将它包含到我的代码中吗?...因为我不知道
  • 分享您的日记日期字段代码或完整的gridview代码不需要c#代码
  • 如果您不使用模板字段,只使用绑定字段,那么您可能有两个用于日记日期的绑定字段
  • 我已将其添加到主帖的顶部...仅此而已.. 没有更多代码来填充我只使用 C# 的 gridview ..基本上我已经发布了整个代码
  • @Diego Morales Sepulveda 很高兴为您提供帮助1
【解决方案2】:

在 BoudField 中,试试这种格式:

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

输出:

01/06/2017 00:00:00

【讨论】:

  • 那不起作用..因为我没有使用 BOUNDFIELD
  • 你在用mysql吗?
【解决方案3】:

如果你在你的 SQL 查询中添加下面的Convert 函数,你的目的就达到了。例如:

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

【讨论】:

    【解决方案4】:

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

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

    它会给你

    17 年 1 月 6 日

    【讨论】:

    • SQL表中存储的数据是DATE格式..被gridview转换成日期时间..所以这个解决方案行不通..谢谢大佬
    猜你喜欢
    • 2013-01-15
    • 2017-06-20
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    相关资源
    最近更新 更多