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

  在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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值