calendar column c# in datagridview

mickro picture mickro · Aug 19, 2013 · Viewed 17.1k times · Source

Basically I want to show a calendar when I edit some columns in my Datagridview.

Following How to: Host Controls in Windows Forms DataGridView Cells from MSDN I can add wanted kind of column at building.

But in my case, I have to use a datasource provided by an Excel reader which give headings have to be connected to date type columns.

private DataGridView AddCalendars(DataGridView dtgv)
{
  dtgv.DataSource = controller.getEmptyDataTable();     // DataTable provided by excel reader
  var l = dtgv.Columns.Count;
  string[] dateColumns = {"date_received", "date_of_birth"};
  for (var i = 0; i < l; ++i)
  {
    if ( dateColumns.Any( dtgv.Columns[i].HeaderText.Contains )
    {
      dtgv.Columns[i] = new CalendarColumn();      // this line does not work cause by readonly
    }
  }
  return dtgv;
}

How can I apply Calendar Column control to selected columns ?

Or, how can I obtain same result by building Datagridview different way ?

Answer

mickro picture mickro · Aug 20, 2013

find an acceptable solution working around this post http://www.codeproject.com/Questions/175124/placing-datetimepicker-in-datagridview

private DateTimePicker cellDateTimePicker;
private List<int> dateColumnsIndexes;

public MainForm()
{
  InitializeComponent();

  /// 
  this.cellDateTimePicker = new DateTimePicker();
  this.cellDateTimePicker.ValueChanged += new EventHandler(cellDateTimePickerValueChanged);
  this.cellDateTimePicker.Visible = false;
  this.cellDateTimePicker.CustomFormat = "dd/MM/yyyy";
  this.cellDateTimePicker.Format = DateTimePickerFormat.Custom;
  this.dataGridView1.Controls.Add(cellDateTimePicker);
  (...)
}

private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
  var index = masterDataGridView.CurrentCell.ColumnIndex;
  if (this.dateColumnsIndexes.Contains(index))
  {
    Rectangle tempRect = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);          
    cellDateTimePicker.Location = tempRect.Location;
    cellDateTimePicker.Width = tempRect.Width;
    try
    {
      cellDateTimePicker.Value = DateTime.Parse(dataGridView1.CurrentCell.Value.ToString());
    }
    catch
    {
      cellDateTimePicker.Value = DateTime.Now;
    }
    cellDateTimePicker.Visible = true;
  }
}

void cellDateTimePickerValueChanged(object sender, EventArgs e)
{
  masterDataGridView.CurrentCell.Value = cellDateTimePicker.Value.ToString("dd/MM/yyyy");
  cellDateTimePicker.Visible = false;
}

private void AddCalendars(DataGridView dtgv)
{
  dateColumnsIndexes = new List<int>();
  dtgv.DataSource = controller.getEmptyDataTable();     // DataTable provided by excel reader
  var l = dtgv.Columns.Count;
  string[] dateColumns = {"date_received", "date_of_birth"};
  for (var i = 0; i < l; ++i)
  {
    if ( dateColumns.Any( dtgv.Columns[i].HeaderText.Contains )
    {
      dateColumnsIndexes.add(i);
    }
 }

}