【发布时间】:2021-09-18 07:47:26
【问题描述】:
当我在datagridview 中选择一行时,我希望我的数据库中的日期显示在我的应用程序中的DateTimePicker 上。
但我遇到了一个错误,告诉我'String was not recognized as a valid DateTime.'
由于 MySQL 采用 'yyyy-MM-dd' 格式,我将 DateTimePicker 格式更改为相同。
我尝试将DataGridView的值提取为字符串,发现我得到的格式是"dd-MM-yyyy hh:MM:ss t"
我能做些什么来解决它?
private void dtv_dis1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
txt_id.Text = dtv_dis1.SelectedRows[0].Cells[0].Value.ToString();
txt_amt.Text = dtv_dis1.SelectedRows[0].Cells[6].Value.ToString();
txt_remarks.Text = dtv_dis1.SelectedRows[0].Cells[5].Value.ToString();
//Error with date
String date = dtv_dis1.SelectedRows[0].Cells[1].Value.ToString();
dtp_date.Value = DateTime.ParseExact(date, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
//textBox1.Text = dtv_dis1.SelectedRows[0].Cells[1].Value.ToString();
tot = dtv_dis1.SelectedRows[0].Cells[2].Value.ToString();
mop = dtv_dis1.SelectedRows[0].Cells[4].Value.ToString();
cur = dtv_dis1.SelectedRows[0].Cells[3].Value.ToString();
if (tot == "Domestic")
{
rb_dom.Checked = true;
}
else
{
rb_in.Checked = true;
}
if (mop == "Cash")
{
rb_csh.Checked = true;
}
else if (mop == "Credit Card")
{
rb_cc.Checked = true;
}
else
{
rb_nb.Checked = true;
}
if (cur == "Doller")
{
rb_doller.Checked = true;
}
else if (cur == "Euro")
{
rb_euro.Checked = true;
}
else
{
rb_Rupees.Checked = true;
}
}
【问题讨论】:
-
DateTime值是如何首先进入网格的?如果网格使用像DataTable这样的数据源,那么我会假设日期值列的类型是DateTime…如果不是…,它应该是。网格中日期列的数据类型是什么? -
它的
DATE在网格@JohnG 的日期列中
标签: c# .net visual-studio datagridview datetimepicker