2013-10-01 164 views
0

我有一个dateTime字段,我在GridView中显示。问题是我只需要显示系统的日期而不是时间,但它显示日期和时间。应该采用什么合适的格式?格式化日期时间对象

这将在文本框的日期:

public static List<Workassign> GetTable() 
    { 
     List<Workassign> listValues = new List<Workassign>(); 

     string connectionString = "server=(local);database=project_zeshan;Integrated Security=SSPI"; 
     SqlConnection con = new SqlConnection(connectionString); 

      SqlCommand cmd = new SqlCommand("Select * from Assign_Work", con); 
      con.Open(); 
      SqlDataReader rdr = cmd.ExecuteReader(); 

      while (rdr.Read()) 
      { 
       Workassign ob = new Workassign(); 
       ob.listItem_1 =rdr["listitem_1"].ToString() ; 
       ob.listItem_2 = rdr["listitem_2"].ToString(); 
       ob.Description = rdr["Description"].ToString(); 
       ob.Date= DateTime.Parse(rdr["Date"].ToString()); 
       ob.Image = rdr["Image"].ToString(); 


       listValues.Add(ob); 

      } 


      return listValues; 






    } 

而且gridView.aspx代码:

<form id="form1" runat="server"> 
<div> 
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     CellPadding="4" ForeColor="#333333" GridLines="None" 
     onselectedindexchanged="GridView1_SelectedIndexChanged"> 
     <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
     <Columns> 
      <asp:BoundField DataField="listitem_1" HeaderText="ListItem1" /> 
      <asp:BoundField DataField="listItem_2" HeaderText="listItem2" /> 
      <asp:BoundField DataField="Description" HeaderText="Description" /> 
      <asp:BoundField DataField="Date" HeaderText="Date" /> 
      <asp:TemplateField HeaderText="Image"> 
       <ItemTemplate> 
        <asp:Image ID="Image1" runat="server" Height= "50px" Width = "100px" ImageUrl='<%# Bind("Image") %>' /> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
     <EditRowStyle BackColor="#999999" /> 
     <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
     <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
     <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
     <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
     <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
     <SortedAscendingCellStyle BackColor="#E9E7E2" /> 
     <SortedAscendingHeaderStyle BackColor="#506C8C" /> 
     <SortedDescendingCellStyle BackColor="#FFFDF8" /> 
     <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> 
    </asp:GridView> 
</div> 
</form> 

+0

显示您的代码。 –

回答

2
protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      TextBoxdate.Text = DateTime.Today.ToShortDateString(); 

     } 
    } 

从表中检索10试试这个:

<asp:BoundField DataField="Date" HeaderText="Date" DataFormatString="{0:MM/dd/yyyy}" /> 
+0

很酷的工作!谢谢 – Vector

+0

@novice不客气。 –

0

但是,您可以格式化你的日期时间喜欢使用ToString()从这里选择创建格式字符串:MSDN - Custom Date and Time Format Strings

// For a standard U.S.-style date: 
myDateTime.ToString("MM/dd/yyyy") 

// For ISO-style: 
myDateTime.ToString("yyyy-MM-dd") 

等。

编辑

@JohnH正确地指出,在一个GridView工作的具体情况,你不会用ToString或其他方法,而是提供了一个DataFormatString属性。我将把我的答案留给格式化日期的一般用例以及指向包含可用格式字符串的MSDN资源的链接。

+0

感谢您分享链接兄弟! – Vector