winform datetimepicker 注意事项
1.关于 datetimepicker.Text 如果赋值给该属性的值不能转化为时间类型,那么赋值将失败。
有效的解决办法可以参照如下代码;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 CultureInfo ci = new CultureInfo("zh-cn");
2 string dtpText = dtp.Text;
3 if (dtpText=="")
4 {
5 var dtOther = DateTime.Now;
6 string month = dtOther.ToString("MM",ci);
7 string day = dtOther.ToString("dd",ci);
8 string dayOfWeek = dtOther.ToString("ddd",ci);
9 string ss = string.Format("{0}.{1} {2}", month, day, dayOfWeek);
10 dtpText = ss;
11
12 }
13 string s = dtpText.Substring(0, 5) + dtpText.Substring(7, 1);
14 string format = "MM.dd ddd";
15 DateTime dt = DateTime.ParseExact(dtpText, format, ci);
16 dataGridView1.CurrentCell.Value = s;
17 dtp.Visible = false;
2 string dtpText = dtp.Text;
3 if (dtpText=="")
4 {
5 var dtOther = DateTime.Now;
6 string month = dtOther.ToString("MM",ci);
7 string day = dtOther.ToString("dd",ci);
8 string dayOfWeek = dtOther.ToString("ddd",ci);
9 string ss = string.Format("{0}.{1} {2}", month, day, dayOfWeek);
10 dtpText = ss;
11
12 }
13 string s = dtpText.Substring(0, 5) + dtpText.Substring(7, 1);
14 string format = "MM.dd ddd";
15 DateTime dt = DateTime.ParseExact(dtpText, format, ci);
16 dataGridView1.CurrentCell.Value = s;
17 dtp.Visible = false;