簡體   English   中英

在后面的代碼中設置DataFormatString

[英]Set DataFormatString in code behind

我有一些列的GridView,我需要從代碼后面更改DataFormatString,因為我需要為不同的區域設置不同的值。

這是我的日期專欄,我需要后面代碼的格式。 如果我在這里設置DataFormatString,那就可以了。

<asp:BoundField DataField="LastChange" HeaderStyle-CssClass="gwHeader"
ItemStyle-CssClass="gwItem center"HeaderText="Last change"
DataFormatString="{0:dd.MM.yyyy}"> </asp:BoundField>

但是,如果我嘗試它后面的代碼更改不起作用(數據庫中的原始日期未按DataFormatString中的模式進行格式化)

我試過了:

BoundField lastChange = GridView1.Columns[11] as BoundField;
lastChange.DataFormatString = "{0:MM/dd/yyyy}";

我將其放入Page_Load和GridView1_RowDataBound

謝謝你的幫助。

您可以使用以下代碼從代碼隱藏中進行設置:

public static string myFormatString = "{0:MM/dd/yyyy}";

((BoundField)GridView1.Columns[ColumnIndex]).DataFormatString = myFormatString;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM