[csharp] view plaincopyprint?DateTimePicker dtp = new DateTimePicker();

http://blog.csdn.net/diyoosjtu/article/details/7584857

 

由于DataGridView自带的ColumnType里面没有DateTimePicker这个控件。所以要实现一个输入日期的列就比较麻烦了。通过以下方法可以往DataGridView加入DateTimePicker控件。

首先,前端设计加入一个DataGridView控件,命名为DataGridView1。(文/piikee

然后,后台.cs文件写入以下代码:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace moonlight_treasure  
  11. {  
  12.         
  13.     public partial class MyCount : Form  
  14.     {  
  15.         DateTimePicker  dtp = new DateTimePicker();  //这里实例化一个DateTimePicker控件   
  16.          Rectangle _Rectangle;  
  17.   
  18.         public MyCount()  
  19.         {  
  20.             InitializeComponent();  
  21.             dataGridView1.Controls.Add(dtp);  //把时间控件加入DataGridView   
  22.             dtp.Visible = false;  //先不让它显示   
  23.             dtp.Format = DateTimePickerFormat.Custom;  //设置日期格式为2010-08-05   
  24.             dtp.TextChanged += new EventHandler(dtp_TextChange); //为时间控件加入事件dtp_TextChange   
  25.         }  
  26.   
  27.   
  28. /*************时间控件选择时间时****************/  
  29.         private void dtp_TextChange(object sender, EventArgs e)  
  30.         {  
  31.             dataGridView1.CurrentCell.Value = dtp.Text.ToString();  //时间控件选择时间时,就把时间赋给所在的单元格   
  32.                     }  
  33.   
  34. /****************单元格被单击,判断是否是放时间控件的那一列*******************/  
  35.         private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)  
  36.         {  
  37.   
  38.             if (e.ColumnIndex == 0)  
  39.             {  
  40.                 _Rectangle = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true); //得到所在单元格位置和大小   
  41.                 dtp.Size = new Size(_Rectangle.Width, _Rectangle.Height); //把单元格大小赋给时间控件   
  42.                 dtp.Location = new Point(_Rectangle.X, _Rectangle.Y); //把单元格位置赋给时间控件   
  43.                 dtp.Visible = true;  //可以显示控件了   
  44.             }  
  45.             else  
  46.                 dtp.Visible = false;  
  47.         }  
  48.   
  49. /***********当列的宽度变化时,时间控件先隐藏起来,不然单元格变大时间控件无法跟着变大哦***********/  
  50.         private void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)  
  51.         {  
  52.             dtp.Visible = false;  
  53.               
  54.         }  
  55.   
  56. /***********滚动条滚动时,单元格位置发生变化,也得隐藏时间控件,不然时间控件位置不动就乱了********/  
  57.         private void dataGridView1_Scroll(object sender, ScrollEventArgs e)  
  58.         {  
  59.             dtp.Visible = false;  
  60.         }  
  61.   
  62.   
  63.     }  
  64. }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值