0
votes

I have a datetime column in datagridview and I display it in custom format: dd/MM/yyyy HH:mm:ss

I want to edit datetime value, so I tried adding a datetimepicker control and it works if the values are properly choosed. (yet I don't feel it is efficient).

Sometimes the invalid values are accepted and dgv_dataerror event occurs infinitely without letting me to change the value.

For example, when I keeping moving from one cell to another using keyboard arrow keys, accidentally if I press space bar, the cell value becomes empty and dataerror event occurs infinitely.

Is there any alternative solution to achieve this without datetimepicker control for changing the datetime value?

1

1 Answers

1
votes

I found the answer here: https://msdn.microsoft.com/en-us/library/7tas5c80%28v=vs.110%29.aspx

It creates a type of DataGridViewColumn with the DateTime picker...

Edit: How to add this custom column dynamically:

        dgvMain.DataSource = bulkImportTable;
        CalendarColumn col = new CalendarColumn();
        col.Name = "DeployDate";
        dgvMain.Columns.Remove("DeployDate");
        dgvMain.Columns.Add(col);

        col.DataPropertyName = col.Name;
        foreach (DataGridViewRow row in dgvMain.Rows)
        {
            row.Cells[dgvMain.Columns.Count - 1].Value = DateTime.Now.ToShortDateString();
        }

In this code I use the ToShortDateString(), but you can use your own custom method here. Be sure to also change this custom code in the customized CalendarColumn... THe advatage of using ShortDateString and LongDateString, is that this changes dynamically when the client has a different regional setting...