我在数据库中有一些字段,其中两个是日期时间字段,我通过 GridView 中的“DataTable”检索它,如下面的代码所示
C# 代码
var connection = new MySqlConnection(_TestMySqlConString);
string query1 = myQuery;
var cmd1 = new MySqlCommand(query1, connection);
connection.Open();
var dataTable1 = new DataTable();
var da1 = new MySqlDataAdapter(cmd1);
da1.Fill(dataTable1);
grdInvitationTendersList.DataSource = dataTable1;
grdInvitationTendersList.DataBind();
ASP 代码
<asp:GridView ID="grdList" runat="server"
AllowSorting="True" AutoGenerateColumns="False"
DataKeyNames="R_NUMBER" OnRowCommand="grdList_RowCommand" >
<AlternatingRowStyle BackColor="White" />
<Columns>
....
<asp:BoundField DataField="TENDER_NUMBER" HeaderText="T_NUMBER" ReadOnly="True" SortExpression="T_NUMBER" Visible="TRUE"></asp:BoundField>
<asp:BoundField DataField="LAST_DATE_SUBMISSION" HeaderText="Last Date of Submission" ReadOnly="True" SortExpression="LAST_DATE_SUBMISSION" HeaderStyle-BorderStyle="Solid"
HeaderStyle-HorizontalAlign="Right" FooterStyle-BackColor="pink" FooterStyle-BorderColor="White" Visible="True" DataFormatString="{0:g}"></asp:BoundField>
......
</Columns>
LAST_DATE_SUBMISSION
是一个 dateTime
字段。
我的问题是,如何才能获得 yyyy/MM//dd hh:mm:ss
中的日期时间,我必须转换文化
并显示日期时间在 GridView 中
*编辑1:* 我承认我的问题还没有清除。让我解释一下,我的问题的第二部分是“我必须将日期时间转换为其他日历类型,然后显示在 GridView 中”
最佳答案
DateTime
值本质上不具有区域性。这只是一个值。我怀疑你应该改变这一点:
DataFormatString="{0:g}"
例如,将其更改为:
DataFormatString="{0:yyyy/MM/dd HH:mm:ss}"
(假设您实际上想要 24 小时 View ,请注意 HH
而不是 hh
。同样,我假设您的 //
问题是一个拼写错误。)
如果您想将其硬编码为特定格式。如果您确实想要“适合区域性的任何格式”,那么您应该在处理请求时确保当前区域性是正确的,并且您的原始 DataFormatString
是合适的。
编辑:我现在也看到您想要更改日历系统 - 在这种情况下,它实际上是为了确保您使用正确的文化。 DateTime
值没有日历系统;日历系统由其显示的文化决定。
另一种方法是使用我的 Noda Time库 - 将值转换为 LocalDateTime
以及您想要的任何日历,可以独立于文化进行显示。
关于c# - 如何更改 DataTable 或 GridView 中日期时间的日期文化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21596355/