我在 GridView 中显示了一个 dateTime 字段。问题是我只需要显示系统的日期而不是时间,而是显示日期和时间。应该应用什么适当的格式?
这会在文本框中设置日期:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBoxdate.Text = DateTime.Today.ToShortDateString();
}
}
从表中检索:
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>