How to get correct datetime format in dateTimePicker after double click on datagridview row without parsing

Multi tool use
How to get correct datetime format in dateTimePicker after double click on datagridview row without parsing
I am working on windows form application.
It contains two form MainForm - Test and childForm - Search. Search form has dataGridView control.
And Main form contain dateTimePicker1 control.
My Question is - When I double click on row from datagridview, my dateTimePicker shows the date in dd/MM/yyyy format as shown in above. But it shows in MM/dd/yyyy format.
DateTimePicker control properties
this.dateTimePicker1.CustomFormat = "dd/MM/yyyy";
this.dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
this.dateTimePicker1.Name = "dateTimePicker1";
this.dateTimePicker1.Value = new System.DateTime(2018, 6, 21, 0, 0, 0, 0);
Main Form Code :
private void SearchTestToolStripMenuItem_Click(object sender, EventArgs e)
{
using (var searchForm = new SearchTest())
{
DataRow currentRow = null;
if (searchForm.ShowDialog() == DialogResult.OK)
{
if (searchForm.dataGridView1.CurrentRow != null)
currentRow = ((DataRowView)searchForm.dataGridView1.CurrentRow.DataBoundItem).Row;
if (currentRow != null)
{
tb_SerialNo.Text = (string)currentRow["SrNo"];
tb_TypeNo.Text = (string)currentRow["TypeNo"];
tb_TestEngineer.Text = (string)currentRow["TestEngineer"];
dateTimePicker1.Value = (DateTime)currentRow["Date"]; // here I am getting date from gridview in dd/MM/yyyy format
}
}
dt.DefaultView.RowFilter = "";
}
}
private void ReadFile()
{
dt = new DataTable("Model1");
dt.Columns.Add("SrNo", typeof(string));
dt.Columns.Add("TypeNo", typeof(string));
dt.Columns.Add("TestEngineer", typeof(string));
dt.Columns.Add("Date", typeof(DateTime));
dt.ReadXml(filePath);
}
SearchTest Form Code:
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
this.Close();
}
My XML data:
<SrNo>AB_1</SrNo>
<TypeNo>CSE104</TypeNo>
<TestEngineer>JR</TestEngineer>
<Date>2018-02-07T00:00:00+05:30</Date>
I don't want to use ParseExact method.
To not get confused about day and month, test your code with a date like
25/12/2018
.– Reza Aghaei
Jul 2 at 10:58
25/12/2018
Its in yyyy-dd-MM format. Its today's date.
– J.rao
Jul 2 at 10:59
Actually problem is in this line of code - dateTimePicker1.Value = (DateTime)currentRow["Date"]; ..(DateTime)currentRow["Date"] this gives me dd/MM/yyyy format and dateTimePicker1.Value its in MM/dd/yyyy format
– J.rao
Jul 2 at 11:00
Try this:
var m = DateTime.Parse("2018-02-07T00:00:00+05:30").Month; MessageBox.Show(m.ToString());
and see the month.– Reza Aghaei
Jul 2 at 11:03
var m = DateTime.Parse("2018-02-07T00:00:00+05:30").Month; MessageBox.Show(m.ToString());
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
The date that you have in the XMl file is 7th of February. All other things seem working well.
– Reza Aghaei
Jul 2 at 10:53