1

Im using MySQL connector to receive data form database and put values to dataset->dataGridView. Then i need to change value in date column when i click on column with checkbox:

   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");
        }
    }

I need to convert DateTime to MySqlDateTime, to insert value in some column. Now i have error, that expects MySqlData type. Unable to convert System.DateTime value to MySQL date/time

user2516034
  • 155
  • 3
  • 3
  • 8

1 Answers1

5

MySQL DateTime is in this format: yyyy-MM-dd HH:mm:ss.

The following code will take your C# DateTime and convert it to MySQL DateTime:

DateTime dateValue = DateTime.Now;
string MySQLFormatDate = dateValue.ToString("yyyy-MM-dd HH:mm:ss");
// Now write it to your database...
Derek W
  • 9,708
  • 5
  • 58
  • 67