This article shows how to embed a Calendar (DataTimePicker) Control into a cell of a DataGridView Winform control.
In a DataGridView if a cell contains data in a DateTime Format and you want to edit the date directly on the grid, by default the Winform DataGridView control doesn't provide a Calender (DateTimePicker) control on that cell. However this feature is easily available with various third-party Winform Controls (like Infragistics controls). To show a Calendar on the DataGridView Cell, here we include a DataGridView on the form and populate the data by Binding a list of objects. After creating the data source, we create a DateTimePicker control programmatically whenever a cell with a date is clicked.
A sample project is also attached for better understanding.
A Sample class to hold the TestData and to bind into DataGridView Control
To show sample data on the DataGridView, here I created a sample class MyData with the three properties: ID, Date and Name.
-
-
-
- class MyData
- {
- public int ID { get; set; }
- public DateTime Date { get; set; }
- public string Name { get; set; }
-
- public MyData(int id, DateTime dt, string name)
- {
- ID = id;
- Date = dt;
- Name = name;
- }
- }
On the FormLoad method, I created a List of MyData objects (an IEnumerable object) to bind as a Data Source into the DataGridView Control.
- private void MainForm_Load(object sender, EventArgs e)
- {
-
-
- List<MyData> oMyDataList = new List<MyData>();
-
- MyData obj1 = new MyData(1, DateTime.Now, "John");
- MyData obj2 = new MyData(2, DateTime.Now, "Sam");
- MyData obj3 = new MyData(3, DateTime.Now, "Ray");
-
- oMyDataList.Add(obj1);
- oMyDataList.Add(obj2);
- oMyDataList.Add(obj3);
-
-
- dataGridView1.DataSource = oMyDataList;
- }
Displaying a Calendar (DateTimePicker) Control on Cell
When the cell containing Date field is clicked, I create a new DateTimePicker Control programmatically and set the Size, Location, Format properties and so on. Two events "CloseUp" and "TextChanged" are also attached with this control.
- private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
- {
-
- if (e.ColumnIndex == 1)
- {
-
- oDateTimePicker = new DateTimePicker();
-
-
- dataGridView1.Controls.Add(oDateTimePicker);
-
-
- oDateTimePicker.Format = DateTimePickerFormat.Short;
-
-
- Rectangle oRectangle = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
-
-
- oDateTimePicker.Size = new Size(oRectangle.Width, oRectangle.Height);
-
-
- oDateTimePicker.Location = new Point(oRectangle.X, oRectangle.Y);
-
-
- oDateTimePicker.CloseUp += new EventHandler(oDateTimePicker_CloseUp);
-
-
- oDateTimePicker.TextChanged += new EventHandler(dateTimePicker_OnTextChange);
-
-
- oDateTimePicker.Visible = true;
- }
- }
Retrieving the selected date from Calendar and saving into DataGrid CellOn the TextChanged event handler I get the selected date on the calendar and save it into the corresponding DataGridView cell.
- private void dateTimePicker_OnTextChange(object sender, EventArgs e)
- {
-
- dataGridView1.CurrentCell.Value = oDateTimePicker.Text.ToString();
- }
Hiding the Calendar control after its useThis is also an important step because we don't to see the calendar control on the cell once the date has been selected on the calendar. To do this, we hide it on the "CloseUp" event handler as follows:
- void oDateTimePicker_CloseUp(object sender, EventArgs e)
- {
-
- oDateTimePicker.Visible = false;
- }