datetime值从datagridview更新到c#中的数据库
问题描述:
当我在Datagrid视图中选择Date时更新到Database中这里显示一个错误:该字符串未被识别为有效的DateTime。从索引0开始有一个未知单词。
-------------------------------- ----------------------
这是我的代码:
When I select Date in Datagrid view Column for update into Database Here is shown an Error: The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.
------------------------------------------------------
Here is my Code :
private void DataGridDataInsert()
{
DataGridViewRow dgr;
int i;
string Pname,amount,inerID ;
DateTime insertdt;
//double amount;
for (i = 0; i < (dtProductTray.Rows.Count - 1); i++)
{
dgr = dtProductTray.Rows[i];
//Shown Error : in below line:
insertdt= Convert.ToDateTime(dgr.Cells[2].Value);//The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.
amount = dgr.Cells[3].Value.ToString();
inerID =dgr.Cells[0].Value.ToString();
DB_Manager.UpdateData("update datatable set insert_date='" +Convert.ToDateTime( insertdt.ToString("MM/dd/yyyy")) + "',amount=" + amount.ToString() + " where iner_id=" + inerID + " ");
}
}
Plz help
Plz help
答
调试看看是什么值为dgr.Cells [2]。值。
Debug and see what is the value of dgr.Cells[2].Value.
引用:
好的,这是单元索引问题,它将是细胞[1]而不是细胞[2]。所以它解决了
Ok, it was cell index problem, it would be Cells[1] itstead of Cells[2]. so it is solved