2013-06-24 36 views
0

我使用MySQL連接器來接收數據表單數據庫並將值設置爲dataset> dataGridView。然後,我需要更改日期列的值時,我與複選框,點擊列:C#DateTime到MySqlDateTime轉換

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
    { 
     if (dataGridView1.Columns[e.ColumnIndex].Name == "Продано") 
     { 
      DataGridViewCheckBoxCell checkbox = (DataGridViewCheckBoxCell)dataGridView1.CurrentCell; 
      bool isChecked = (bool)checkbox.EditedFormattedValue; 
      DateTime dt = DateTime.Today; 
      dataGridView1.Rows[e.RowIndex].Cells[4].Value = dt.ToString("yyyy-MM-dd"); 
     } 
    } 

我需要的DateTime轉換爲MySqlDateTime,在一些列中插入值。 現在我有錯誤,期望MySqlData類型。 Unable to convert System.DateTime value to MySQL date/time

+0

哪條線導致錯誤? –

回答

2

MySQL DateTime的格式如下:yyyy-MM-dd HH:mm:ss

下面的代碼將你的C#的DateTime並將其轉換爲MySQL的日期時間

DateTime dateValue = DateTime.Now; 
string MySQLFormatDate = dateValue.ToString("yyyy-MM-dd HH:mm:ss"); 
// Now write it to your database...