Click here to Skip to main content
15,891,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When a cell is edited in datagridview, I want to place datetimepicker in that cell.
I have written this piece of code but this doesn't seem to work.

Can any one help me in this?


C#
void DataGridView1DoubleClick(object sender, EventArgs e)
        {
MouseEventArgs args = (MouseEventArgs)e;
Point p = dataGridView1.PointToClient(new Point(args.X,args.Y));
DataGridView.HitTestInfo info = dataGridView1.HitTest(p.X, p.Y);
int Col=dataGridView1.CurrentCellAddress.X;
if(dataGridView1.Columns[Col].Name == "Employee name";)
            {
//              textBox1.text="hello";
//              textBox1.SetBounds(args.X,args.Y,dataGridView1.CurrentCell.Size.Width,dataGridView1.CurrentCell.Size.Height);
//                  textBox1.Show();
//                  DataGridTextBox.TextBox.Controls.Add( textBox1 );
//                textBox1.Text = DataGridView[DataGridView1.CurrentRowIndex , Col].ToString();
//                textBox1.Focus();
            }
            if(Col==3)
            {
                 //placing date time picker here..
            }
        }



thanks in advance.
Posted
Updated 31-Mar-11 4:31am
v3
Comments
Dalek Dave 31-Mar-11 10:32am    
Edited for Grammar and Readability.
girish sp 1-Apr-11 0:45am    
thanks dave..

Try the code specified below

C#
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int myRow = 2;  // we'll place this functionality
            int myCol = 2;  // in the 2nd row and column of the control

            if ((e.RowIndex == myRow) & (e.ColumnIndex == myCol))
            {
                DateTimeForm myDTForm = new DateTimeForm();
                if (myDTForm.ShowDialog()== DialogResult.OK)
                {
                    // put the dialog result into the datagridview cell
                    ((DataGridView)sender)[myCol, myRow].Value = myDTForm.Tag;
                    ((DataGridView)sender).Invalidate(); // to update the grid
                }
            }
        }


I did not test this code. I found it while I was searching for some other query.
If that is not working let me know so that I can try fixing the code.
 
Share this answer
 
v3
Comments
girish sp 31-Mar-11 9:00am    
i have not tried this snippet..maybe tomorrow i will try this code..and let you know..i will reply to you tomorrow..
girish sp 31-Mar-11 9:07am    
thanks for your valuable reply..
girish sp 1-Apr-11 1:00am    
there is no property called DateTimeForm!!its giving error.should i include any namespace??
do reply
Dalek Dave 31-Mar-11 10:32am    
Good Call
Ngomana FT 23-Oct-17 8:54am    
it doesnt work
i have come up with this code, working for me..thought of posting it here..it might help some one..any feedback on this code are welcome..
C#
        private DateTimePicker cellDateTimePicker;
        public MainForm()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();
            this.cellDateTimePicker = new DateTimePicker();
            this.cellDateTimePicker.ValueChanged += new EventHandler(cellDateTimePickerValueChanged);
            this.cellDateTimePicker.Visible = false;
            this.dataGridView1.Controls.Add(cellDateTimePicker);
         
            if (e.ColumnIndex == column index which u want)

            {

                Rectangle tempRect = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);

                cellDateTimePicker.Location = tempRect.Location;

                cellDateTimePicker.Width = tempRect.Width;

                cellDateTimePicker.Visible = true;

            }

void cellDateTimePickerValueChanged(object sender, EventArgs e)
        {
            dataGridView1.CurrentCell.Value = cellDateTimePicker.Value.ToString();//convert the date as per your format
            cellDateTimePicker.Visible = false;
    }
 
Share this answer
 
v2
Comments
Toniyo Jackson 31-Mar-11 9:48am    
Use the pre tag correctly
sagar.kandikatla 25-Nov-13 3:58am    
how can i handle the left right key to change the value of date using key board please help me
girish sp 1-Apr-11 3:11am    
sure..
Dalek Dave 31-Mar-11 10:33am    
Good Call
girish sp 1-Apr-11 3:10am    
thanks
Solution 2 is excellent. Here is some clarification. This code could be called for CellClick(). CellContentClick(), CellContentDoubleClick(), and CellDoubleClick(). This code is tested and works in VS2012.
C#
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == column index which u want)

    {


        Rectangle tempRect = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);

        cellDateTimePicker.Location = tempRect.Location;

        cellDateTimePicker.Width = tempRect.Width;

        cellDateTimePicker.Visible = true;

    }

}
 
Share this answer
 
Comments
Shakeeb Sadikeen 7-Sep-15 10:06am    
Better use CellBeginEdit Event
Member 10186275 17-Nov-18 14:31pm    
celldatetimepicker give me a red underline how can i solve it
Also see this from MSDN:

http://msdn.microsoft.com/en-US/library/7tas5c80(v=vs.110).aspx[^]

Although in the Form1 constructor, it appears that this line
C#
this.Load += new EventHandler(Form1_Load);

Results in the Load event being called twice. Not sure what the purpose of this is supposed to be, but when it is removed the example works fine.
 
Share this answer
 
Comments
Maciej Los 6-Nov-14 17:52pm    
It's very old question. The rule is: do not answer so old questions.
Please, delete this answer to avoid down-voting.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900