0

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?

CST RAIZE
  • 428
  • 1
  • 5
  • 18

1 Answers1

1

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...

Rahvin47
  • 91
  • 5
  • This is what i was expecting. But, here the column is added dynamically. How do I achieve this behaviour if the datagridview is populated from datatable? – CST RAIZE Dec 30 '15 at 18:21
  • How do I apply this calender cell template to the datetime column in datagridview ? I tried setting the calender cell template for the datetime column, it works only after sorting any column in datagridview. dgv.Invalidate() is not doing the job. – CST RAIZE Jan 05 '16 at 05:36
  • By removing and adding the Column...Maybe you can post your code? – Rahvin47 Jan 05 '16 at 09:48
  • http://stackoverflow.com/questions/34550178/c-custom-celltemplate-is-not-invalidating-in-datagridview Check this link, I have posted a question separately. Please help. – CST RAIZE Jan 06 '16 at 11:52