将datetimepicker放在datagridview中 [英] placing datetimepicker in datagridview

查看:111
本文介绍了将datetimepicker放在datagridview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在datagridview中编辑单元格时,我想将datetimepicker放在该单元格中。

我写了这段代码,但这似乎不起作用。



任何人都可以帮我吗?





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?


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.

推荐答案

尝试下面指定的代码



Try the code specified below

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.


我已经想出了这个代码,为我工作......在这里发布..可能会帮助一些..欢迎任何有关此代码的反馈..

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..
        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;
    }


解决方案2非常好。这是一些澄清。可以为CellClick()调用此代码。 CellContentClick(),CellContentDoubleClick()和CellDoubleClick()。此代码经过测试,可在VS2012中使用。

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.
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;

    }

}


这篇关于将datetimepicker放在datagridview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆