2016-09-26 205 views
0

我已经在sql服务器中设置日期数据类型的日期,但是当我想要从中获取值时,将输出日期时间值“2016/9/22 12:00:00 AM”。无法转换从SQL数据库中获得的日期值

DataGridViewRow row = cell.OwningRow; 
string DATE = row.Cells[2].Value.ToString(); 

enter image description here

回答

3

你需要把它格式化在前端:

((DateTime)row.Cells[2].Value).ToString("dd-MM-yyyy"); 
+0

这试着走出错误之前“无重载方法‘的ToString’需要1参数“ –

+0

您需要先将其转换为DateTime - 请参阅我的编辑 – CSL

2

好吧,你usting ToString()方法,它返回string值。你可以用explicit casting检索DateTime

DataGridViewRow row = cell.OwningRow; 
DateTime DATE = (DateTime) row.Cells[2].Value; 
0

转换日期为所需的格式

DateTime DATE = Convert.ToDateTime(row.Cells[2].Value.ToString(); 

当你想要显示的日期,使用格式“YYYY-MM-DD”(根据您的屏幕截图)。

0

转换字符串转换成datetime和你能给格式像这样...... 将工程....

DateTime d=Convert.ToDateTime(row.Cells[2].Value.ToString()); 
textBox1.Text = d.ToShortDateString(); 
相关问题