C#在datagridview单元格中增加日期控件,实现单元格日期控件下拉

来源:互联网 发布:怎么投诉淘宝网站 编辑:程序博客网 时间:2024/05/16 19:39

  在VS2005的datagridview的单元格中,可以设置单元格的下来方式为文本格式,下拉框格式,等,但是没有单元格的下拉,贴出代码让大家参考一下:

在本人测试后VS2008同样适用,注意先申请一个datetimepick的控件dmg.

 private void bibie_Load(object sender, EventArgs e)        {            //status(true);            dmg.Value = System.DateTime.Now;            this.dataGridView1.Controls.Add(dmg);                        dmg.Visible = false;        }
private void dataGridView1_CellBeginEdit(object sender,EventArgs e) // private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)        {            try            {                if (this.dataGridView1.CurrentCell.ColumnIndex.ToString() == "4" || this.dataGridView1.CurrentCell.ColumnIndex.ToString() == "6" || this.dataGridView1.CurrentCell.ColumnIndex.ToString() == "5")//在此指定和哪一列绑定                {                    System.Drawing.Rectangle rect = dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex, false);                    dmg.Left = rect.Left;                    dmg.Top = rect.Top;                    dmg.Width = rect.Width;                    dmg.Height = rect.Height;                    dmg.Visible = true;                    i = this.dataGridView1.CurrentRow.Index;                     j = this.dataGridView1.CurrentCell.ColumnIndex;                     dataGridView1.CurrentCell.Value = dmg.Value;                }                else                {                    dmg.Visible = false;                }                            }            catch            {            }        }

最后的效果:

 

转自于:http://blog.csdn.net/sky_cat/article/details/7850088

原创粉丝点击