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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sze JngPosted Sep 5, 2017, 3:13 AM
Syed MuhammadPosted Dec 4, 2016, 2:35 AM
Hemant SrivastavaPosted Oct 10, 2014, 2:24 PM
You may download the attached sample project for more understanding
Hemant SrivastavaPosted Oct 10, 2014, 2:23 PM
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
Munesh SharmaPosted Sep 22, 2014, 3:34 AM