在MySql数据库中显示dateTime列到C#中的GridView

时间:2016-10-18 12:57:41

标签: c# asp.net gridview

我在将mysql数据库中的列显示到gridView时遇到问题。列是DateTime,sql格式是这样的“2016-03-09 05:09:00”。我面临的问题是其他列显示在gridView上,而不是来自Mysql数据库的dateTime。

以下是从数据库中获取列的代码:

string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (MySqlConnection con = new MySqlConnection(constr))
        {
            using (MySqlCommand cmd = new MySqlCommand("SELECT app_number, app_datetime, app_confirm, pat_ID, rec_ID, doc_ID FROM appointment"))

            {
                using (MySqlDataAdapter sda = new MySqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                    }
                }
            }
        }

这是gridView:        

  <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />

      

  <asp:BoundField DataField="app_number" HeaderText="App#" ReadOnly="true" />

  <asp:TemplateField HeaderText="App Date">
      <ItemTemplate>
        <%# Eval("app_datetime")%>
      </ItemTemplate>
      <EditItemTemplate>
        <asp:TextBox runat="server" ID="txtApp_datetime" Text='<%# Eval("app_datetime")%>' />
      </EditItemTemplate>
  </asp:TemplateField>

  <asp:TemplateField HeaderText="Status">
      <ItemTemplate>
          <%# Eval("app_confirm")%>
      </ItemTemplate>
      <EditItemTemplate>
          <asp:TextBox runat="server" ID="txtConfirm" Text='<%# Eval("app_confirm")%>' />
      </EditItemTemplate>
  </asp:TemplateField>


  <asp:BoundField DataField="pat_ID" HeaderText="Patient ID" ReadOnly="true" />
   <asp:BoundField DataField="rec_ID" HeaderText="Receptionist" ReadOnly="true" />
   <asp:BoundField DataField="doc_ID" HeaderText="Doctor" ReadOnly="true" />



</Columns>


  <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
  <RowStyle BackColor="White" ForeColor="#003399" />
  <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
  <SortedAscendingCellStyle BackColor="#EDF6F6" />
  <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
  <SortedDescendingCellStyle BackColor="#D6DFDF" />
  <SortedDescendingHeaderStyle BackColor="#002876" />

     输出我得到 Data that is Displayed in gridView

2 个答案:

答案 0 :(得分:0)

请使用此而不是您的文本希望这有效,

Text='<%# Eval("app_datetime", "{0:dd/MM/yyyy}") %>' />

答案 1 :(得分:0)

 <ItemTemplate>
    <%# string.Format("{0:dd/MM/yyyy}", Eval("app_datetime"))%>
 </ItemTemplate>