我已经在sql服务器中设置日期数据类型的日期,但是当我想要从中获取值时,将输出日期时间值“2016/9/22 12:00:00 AM”。无法转换从SQL数据库中获得的日期值
DataGridViewRow row = cell.OwningRow;
string DATE = row.Cells[2].Value.ToString();
我已经在sql服务器中设置日期数据类型的日期,但是当我想要从中获取值时,将输出日期时间值“2016/9/22 12:00:00 AM”。无法转换从SQL数据库中获得的日期值
DataGridViewRow row = cell.OwningRow;
string DATE = row.Cells[2].Value.ToString();
你需要把它格式化在前端:
((DateTime)row.Cells[2].Value).ToString("dd-MM-yyyy");
好吧,你usting ToString()
方法,它返回string
值。你可以用explicit casting检索DateTime
:
DataGridViewRow row = cell.OwningRow;
DateTime DATE = (DateTime) row.Cells[2].Value;
转换日期为所需的格式
DateTime DATE = Convert.ToDateTime(row.Cells[2].Value.ToString();
当你想要显示的日期,使用格式“YYYY-MM-DD”(根据您的屏幕截图)。
转换字符串转换成datetime和你能给格式像这样...... 将工程....
DateTime d=Convert.ToDateTime(row.Cells[2].Value.ToString());
textBox1.Text = d.ToShortDateString();
这试着走出错误之前“无重载方法‘的ToString’需要1参数“ –
您需要先将其转换为DateTime - 请参阅我的编辑 – CSL