I have a some fields in database and two of them are as DateTime Field, which I am retrieving it via 'DataTable' in the GridView as in below code
C# Code
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 Code
<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
is a dateTime
Field.
My question is that How can I have the DateTime in yyyy/MM//dd hh:mm:ss
, I have to convert the culture
and display the DateTime in the GridView
*EDIT 1: * I admit that my question is not cleared. Let me explain more, My second part of question is that "I have to convert the Datetime to other Calender type and then display in the GridView"
A DateTime
value doesn't have a culture, inherently. It's just a value. I suspect that you should change this though:
DataFormatString="{0:g}"
For example, change it to:
DataFormatString="{0:yyyy/MM/dd HH:mm:ss}"
(Note the HH
rather than hh
, assuming you actually want a 24-hour view. Likewise I assume the //
in your question was a typo.)
That's if you want to hard-code it to the particular format. If you actually want "whatever the appropriate format is for the culture" then you should make sure the current culture is correct while you're processing the request, and your original DataFormatString
is appropriate.
EDIT: I've now seen your about wanting to change calendar system as well - in that case it really is about making sure you're using the right culture. A DateTime
value doesn't have a calendar system; the calendar system is determined by the culture it's displayed in.
An alternative would be to use my Noda Time library - convert the value into a LocalDateTime
with whatever calendar you want, which can be displayed independently of culture.
See more on this question at Stackoverflow