永发信息网

System.FormatException: 该字符串未被识别为有效的 DateTime。 C#运行程序报这个错.求大师.

答案:5  悬赏:0  手机版
解决时间 2021-01-26 13:26
  • 提问者网友:情歌越听越心酸
  • 2021-01-25 23:21
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
button2.Enabled = true;
button3.Enabled = true;
textBox2.Text = dataGridView1.SelectedCells[4].Value.ToString();
comboBox1.Text = dataGridView1.SelectedCells[2].Value.ToString();
comboBox3.Text = dataGridView1.SelectedCells[3].Value.ToString();
textBox3.Text = dataGridView1.SelectedCells[5].Value.ToString();
textBox1.Text = dataGridView1.SelectedCells[0].Value.ToString();

DateTime a = Convert.ToDateTime(dataGridView1.SelectedCells[1].Value.ToString());
comboBox2.Text = Convert.ToString(a.Year);
comboBox4.Text = Convert.ToString(a.Month);
comboBox5.Text = Convert.ToString(a.Day);

}
最佳答案
  • 五星知识达人网友:蓝房子
  • 2021-01-26 00:20
DateTime a = Convert.ToDateTime(dataGridView1.SelectedCells[1].Value.ToString());
不要使用这种转换方式。
DateTime.TryParse,用这个来做日期转换
全部回答
  • 1楼网友:逐風
  • 2021-01-26 04:52

float y = 8542.12540014;

y.tostring("f2");

  • 2楼网友:woshuo
  • 2021-01-26 03:39
DateTime a = Convert.ToDateTime(dataGridView1.SelectedCells[1].Value.ToString(''YYYY-MM-dd"));
  • 3楼网友:十年萤火照君眠
  • 2021-01-26 02:28
dataGridView1.SelectedCells[1].Value.ToString()的格式不对,检查数据格式是否能转化为DateTime
  • 4楼网友:鸽屿
  • 2021-01-26 01:13
出现这个问题可能的原因: dataGridView1.SelectedCells[1].Value 为null或空字符串或错误的日期,如2009-02-31(2月怎么可能有31天)这样的。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯