1

我使用 MySQL 连接器接收数据表单数据库并将值放入数据集-> dataGridView。然后,当我单击带有复选框的列时,我需要更改日期列中的值:

   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

4

1 回答 1

5

MySQL DateTime 采用这种格式:yyyy-MM-dd HH:mm:ss.

以下代码将获取您的C# DateTime并将其转换为MySQL DateTime

DateTime dateValue = DateTime.Now;
string MySQLFormatDate = dateValue.ToString("yyyy-MM-dd HH:mm:ss");
// Now write it to your database...
于 2013-06-24T14:46:39.440 回答