我正在尝试使用XML作为数据源在C#中绑定网格
string sApplication = txtApplicationsta.Text.Trim();
if (sApplication=="")
{
DataSet ds3 = new DataSet();
ds3.ReadXml(Server.MapPath("Status.xml"));
if (ds3.Tables[0].Rows.Count != 0)
{
gvstatus.DataSource = ds3;
gvstatus.DataBind();
}
}
数据绑定成功。我将日期格式保存为YYYY-MM-DD
。
如下所示:
string idate = DateTime.Parse(ssplit[4].Trim()).ToString("yyyy-mm-dd", CultureInfo.InvariantCulture);
现在我需要以DD-MM-YYYY
格式绑定我的网格列,但是在网格中它会通过我保存的内容进行绑定。
我在Asp页面中绑定网格,如下所示:
<asp:TemplateField HeaderText='Last Update' HeaderStyle-VerticalAlign="Middle">
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" CssClass="chkbox" />
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" att='<%#DataBinder.Eval(Container.DataItem,"ID")%>' Text='<%# SetLinkCodestatus(Convert.ToInt64(DataBinder.Eval(Container.DataItem,"ID")),DataBinder.Eval(Container.DataItem,"LastUpdate").ToString()) %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="3%" HorizontalAlign="left" />
</asp:TemplateField>
如果我以DD-MM-YYYY
格式保存值,如何使用YYYY-MM-DD
格式绑定网格?
答案 0 :(得分:3)
您只需在GridView
列中显示日期值即可完成太多工作。不要使用TemplateField
Label
控件,而是使用简单的BoundField
。
然后,您可以使用dataformatstring
列中的BoundField
媒体资源设置列的日期格式:
<asp:GridView ...>
<Columns>
<asp:BoundField DataField="My_Date_Field" dataformatstring="{0:dd-MM-yyyy}" />
▲
答案 1 :(得分:0)
我们可以借助DataFormatString
实现格式化<asp:BoundField DataField="Amount" HeaderText="Amount" DataFormatString="{0:###,###,##0.000}">