博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#--DataGridView添加DateTimePicker时间控件
阅读量:6955 次
发布时间:2019-06-27

本文共 1631 字,大约阅读时间需要 5 分钟。

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 Test
{
      
    public partial class Form1: Form
    {
        DateTimePicker  dtp = new DateTimePicker();  //实例化一个DateTimePicker控件
         Rectangle _Rectangle;
        public Form1()
        {
            InitializeComponent();
            dataGridView1.Controls.Add(dtp);  //将时间控件加入DataGridView
            dtp.Visible = false;  //先不显示
            dtp.Format = DateTimePickerFormat.Custom;  //设置日期格式,2017-11-11
            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;
        }
    }
}

转载于:https://www.cnblogs.com/turingchang/p/7821663.html

你可能感兴趣的文章
url两次编码
查看>>
正则表达式相关
查看>>
[转]hisi mmz模块驱动讲解
查看>>
二叉树非递归先中后序遍历 及 非递归交换二叉树两个孩子的位置
查看>>
项目总结23:POI生成Excel文件并浏览器导出
查看>>
RabbitMQ 端口号解析
查看>>
当在java不同包中有相同名字的servlet时,在web.xml中该如何配置?
查看>>
仿当当网鼠标经过图片翻转
查看>>
ubuntu 创建桌面快捷方式
查看>>
第三次作业
查看>>
洛谷P5055 【模板】可持久化文艺平衡树(FHQ Treap)
查看>>
【WebApi】通过HttpClient调用Web Api接口
查看>>
iphone-common-codes-ccteam源代码 CCUIViewController.m
查看>>
阿里云乌班图安装JDK\MYSQL\REDIS
查看>>
git冲突解决
查看>>
探索性测试实例-方法篇
查看>>
数论之 莫比乌斯函数
查看>>
AtCoder Regular Contest 096
查看>>
vue-music 关于Search(搜索页面)-- 搜索结果优化
查看>>
Django:URL,Views,Template,Models
查看>>