好,因爲它是一個作爲字符串輸入的日期,至少有兩種方法可以做到這一點。
方法1
數據綁定的字符串作爲Date
對象,而不是String
。由於我現在沒有使用原始日期的確切日期格式,所以您必須自己弄明白日期格式,日/月/年或MM/dd/yyyy。
如果你能做到這一點,只需添加這爲 「綁定列」 元素:
DataFormatString="{0:yyyy/MM/dd}"
或
DataFormatString="{0:yyyy/dd/MM}"
方法2
操縱的RowDataBound oevent文本。我在這裏做了一個粗糙的,可以打磨的。你應該把它們作爲Date
對象來操作。
在ASPX:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
onrowdatabound="GridView1_RowDataBound">
代碼隱藏:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
return;
// assuming the date is at cell index 1
string[] arr = e.Row.Cells[1].Text.ToString().Split('/');
e.Row.Cells[1].Text = string.Format("{0}/{1}/{2}", arr[2], arr[1], arr[0]);
}
是 「1381年12月11日」 的日期正好任何文字? – 2009-12-13 10:17:45
是的。這是日期,但在字符串。 – Mostafa 2009-12-13 10:31:08