I want to add datetimepicker control in predefined column of datagrieview in desktop application.
Plese help.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Hemant SrivastavaRank #581 · 4.2K reputationPosted Oct 10, 2014, 10:23 AM EDT
For a demo purpose I created a class:
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;
}
}
Then on the main Form, I am creating a TestData and binding into DataGrid After that on CellClick event, a DateTimePicker Control is being generated programmatically See the code below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CalanderInDataGridView
{
public partial class MainForm : Form
{
DateTimePicker oDateTimePicker ;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e) oMyDataList = new List();
{
// Creating a Test data
List
MyData obj1 = new MyData(1, DateTime.Now, "John");
MyData obj2 = new MyData(2, DateTime.Now, "Sam");
MyData obj3 = new MyData(3, DateTime.Now, "Ram");
oMyDataList.Add(obj1);
oMyDataList.Add(obj2);
oMyDataList.Add(obj3);
// Binding as a Data Source into DataGridView
dataGridView1.DataSource = oMyDataList;
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
// If any cell is clicked on the Second column which is our date Column
if (e.ColumnIndex == 1)
{
oDateTimePicker = new DateTimePicker(); //DateTimePicker
//Adding DateTimePicker control into DataGridView
dataGridView1.Controls.Add(oDateTimePicker);
// Intially made it invisible
oDateTimePicker.Visible = false;
// Setting the format (i.e. 2014-10-10)
oDateTimePicker.Format = DateTimePickerFormat.Short; //
oDateTimePicker.TextChanged += new EventHandler(dateTimePicker_OnTextChange);
// Now make it visible
oDateTimePicker.Visible = true;
// It returns the retangular area that represents the Display area for a cell
Rectangle oRectangle = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
//Setting area for DateTimePicker Control
oDateTimePicker.Size = new Size(oRectangle.Width, oRectangle.Height);
// Setting Location
oDateTimePicker.Location = new Point(oRectangle.X, oRectangle.Y);
oDateTimePicker.CloseUp += new EventHandler(oDateTimePicker_CloseUp);
}
}
private void dateTimePicker_OnTextChange(object sender, EventArgs e)
{
dataGridView1.CurrentCell.Value = oDateTimePicker.Text.ToString();
}
void oDateTimePicker_CloseUp(object sender, EventArgs e)
{
oDateTimePicker.Visible = false;
}
}
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;
}
}
}
Hope it will help
Hemant SrivastavaRank #581 · 4.2K reputationPosted Oct 10, 2014, 10:24 AM EDT
You may download the attached sample project for more understanding
Munesh SharmaRank #174 · 13.9K reputationPosted Sep 21, 2014, 11:34 PM EDT
Syed Muhammad6 reputationPosted Dec 3, 2016, 9:35 PM EST
Sze Jng3 reputationPosted Sep 4, 2017, 11:13 PM EDT