2016-10-18 43 views
0

我有問題顯示從MySQL數據庫列到GridView。該列是SQL格式的DateTime是這樣的「2016-03-09 05:09:00」。我面臨的問題是其他列會顯示到gridView,而不是來自Mysql數據庫的dateTime。從C#中的MySql數據庫顯示日期時間列到GridView#

下面是從數據庫中獲取的列代碼:

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

+0

MySQL有[適當的數據類型(HTTP://dev.mysql .com/doc/refman/5.7/en/date-and-time-types.html)來存儲日期/時間。你爲什麼不使用這些?然後在.NET中,你可以使用一個'DateTime'對象,你可以顯示你想要的。 – mason

+0

您是否檢查過後端,app_datetime是否有價值? –

+0

問題是,它存儲的格式正確,但我無法顯示使用gridview @mason – johnnitro

回答

0

請使用不是文字希望這個作品的,

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

它不適合我@Kuldip拉娜 – johnnitro

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

沒有區別@Prasanna Kumar J – johnnitro

相關問題