为DataGridView的一个列加入DateTimePicker控件(转)
来源:互联网 发布:restart windows 编辑:程序博客网 时间:2024/06/05 22:49
由于DataGridView自带的ColumnType里面没有DateTimePicker这个控件。所以要实现一个输入日期的列就比较麻烦了。通过以下方法可以往DataGridView加入DateTimePicker控件。
首先,前端设计加入一个DataGridView控件,命名为DataGridView1。
然后,后台.cs文件写入以下代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace moonlight_treasure
{
public partial class MyCount : Form
{
DateTimePicker dtp = new DateTimePicker(); //这里实例化一个DateTimePicker控件
Rectangle _Rectangle;
public MyCount()
{
InitializeComponent();
dataGridView1.Controls.Add(dtp); //把时间控件加入DataGridView
dtp.Visible = false; //先不让它显示
dtp.Format = DateTimePickerFormat.Custom; //设置日期格式为2010-08-05
dtp.TextChanged += new EventHandler(dtp_TextChange); //为时间控件加入事件dtp_TextChange
}
/*************时间控件选择时间时****************/
private void dtp_TextChange(object sender, EventArgs e)
{
dataGridView1.CurrentCell.Value = dtp.Text.ToString(); //时间控件选择时间时,就把时间赋给所在的单元格
}
/****************单元格被单击,判断是否是放时间控件的那一列*******************/
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
_Rectangle = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true); //得到所在单元格位置和大小
dtp.Size = new Size(_Rectangle.Width, _Rectangle.Height); //把单元格大小赋给时间控件
dtp.Location = new Point(_Rectangle.X, _Rectangle.Y); //把单元格位置赋给时间控件
dtp.Visible = true; //可以显示控件了
}
else
dtp.Visible = false;
}
/***********当列的宽度变化时,时间控件先隐藏起来,不然单元格变大时间控件无法跟着变大哦***********/
private void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
{
dtp.Visible = false;
}
/***********滚动条滚动时,单元格位置发生变化,也得隐藏时间控件,不然时间控件位置不动就乱了********/
private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
dtp.Visible = false;
}
}
}
首先,前端设计加入一个DataGridView控件,命名为DataGridView1。
然后,后台.cs文件写入以下代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace moonlight_treasure
{
public partial class MyCount : Form
{
DateTimePicker dtp = new DateTimePicker(); //这里实例化一个DateTimePicker控件
Rectangle _Rectangle;
public MyCount()
{
InitializeComponent();
dataGridView1.Controls.Add(dtp); //把时间控件加入DataGridView
dtp.Visible = false; //先不让它显示
dtp.Format = DateTimePickerFormat.Custom; //设置日期格式为2010-08-05
dtp.TextChanged += new EventHandler(dtp_TextChange); //为时间控件加入事件dtp_TextChange
}
/*************时间控件选择时间时****************/
private void dtp_TextChange(object sender, EventArgs e)
{
dataGridView1.CurrentCell.Value = dtp.Text.ToString(); //时间控件选择时间时,就把时间赋给所在的单元格
}
/****************单元格被单击,判断是否是放时间控件的那一列*******************/
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
_Rectangle = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true); //得到所在单元格位置和大小
dtp.Size = new Size(_Rectangle.Width, _Rectangle.Height); //把单元格大小赋给时间控件
dtp.Location = new Point(_Rectangle.X, _Rectangle.Y); //把单元格位置赋给时间控件
dtp.Visible = true; //可以显示控件了
}
else
dtp.Visible = false;
}
/***********当列的宽度变化时,时间控件先隐藏起来,不然单元格变大时间控件无法跟着变大哦***********/
private void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
{
dtp.Visible = false;
}
/***********滚动条滚动时,单元格位置发生变化,也得隐藏时间控件,不然时间控件位置不动就乱了********/
private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
dtp.Visible = false;
}
}
}
- 为DataGridView的一个列加入DateTimePicker控件(转)
- 为DataGridView的一个列加入DateTimePicker控件
- C# 为DataGridView的一个列加入DateTimePicker控件
- 为DataGridView的一个列加入DateTimePicker控件
- dataGridView控件DateTime列插入DateTimePicker
- DataGridView控件DateTime列插入DateTimePicker
- dataGridView控件DateTime列插入DateTimePicker
- dataGridView控件DateTime列插入DateTimePicker
- Csharp DataGridView自定义添加DateTimePicker控件日期列
- Csharp DataGridView自定义添加DateTimePicker控件日期列
- C# winform DataGridView DateTimePicker 控件
- DataGridView控件的列选中
- VB.NET2008中为datagridview加入下拉树列
- DateTimePicker控件为空
- DataGridView-添加一列为时间,加入时间控件Calender
- (转)VS2005下DataGridView 的多种样式列控件
- 在DataGridView加入CheckBox列
- c#datagridview中添加时间控件DateTimePicker
- Servlet 3.0 新特性概述
- aix系统32位和64位切换
- WINDOWS XP 下Zend Studio For Eclipse 和 Zend Framework 配置
- Java中的instanceof关键字
- android -- WatchDog看门狗分析
- 为DataGridView的一个列加入DateTimePicker控件(转)
- Debug info warn error fatal
- NSArray与NSMutableArray的区别
- Cstring转char、string、int等数据类型的方法
- IIS优化
- ETL架构师面试题
- 获取一个月的第一天及最后一天
- 西蒙iphone-OpenGL ES 教程-13 在 3D中移动
- 理解inode