日期:2014-05-18  浏览次数:20830 次

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

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

 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_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
            {
            }
        }
最后的效果: