WinForm中将时间控件dateTimePicker与数据表格控件DataGridView的某一单元格关联

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u011805979/article/details/80663238

1、在窗口上定义时间控件和数据表格控件,名字分别为dateTimePicker1和dataGridView1;

2、dateTimePicker1设置属性:

            Format:Custom; 

            CustomFormat: HH:mm;

            Visible:false;

            ShowUpDown:true;

3、dataGridView1定义几列,假定第二列为时间显示列,列名:Column2;将其置位底层,以免挡住dateTimePicker1;

        AllowUserToAddRows:false

        AllowUserToDeleteRows:false

4、定义一个全局变量private DataGridViewTextBoxCell seletime;

5、在窗口构造函数中加入如下语句

dateTimePicker1.ValueChanged += new System.EventHandler(timeSele_ValueChanged);//时间控件值改变时的操作

6、定义timeSele_ValueChanged函数

7、定义dataGridView1的CellMouseClick事件 

8、测试代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private DataGridViewTextBoxCell seletime;
        public Form1()
        {
            InitializeComponent();
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;

            for (int i = 0; i < 7; i++)
            {
                dataGridView1.Rows.Add();               
                
                string stime = "00:00";
                dataGridView1.Rows[i].Cells[0].Value = (i+1).ToString();
                dataGridView1.Rows[i].Cells[1].Value = stime;
                dataGridView1.Rows[i].Cells[2].Value = (i + 1).ToString();
            }
            dateTimePicker1.ValueChanged += new System.EventHandler(timeSele_ValueChanged);//时间控件值改变时的操作
        }


        private void timeSele_ValueChanged(object sender, EventArgs e)
        {
            seletime.Value = dateTimePicker1.Value;
            dataGridView1.CurrentCell.Value = dateTimePicker1.Value.ToShortTimeString();
        }
        
        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == 1)//判断是否指定列 
            {
                //设置时间控件的位置             
                dateTimePicker1.Left = dataGridView1.Left + dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).X;
                dateTimePicker1.Top = dataGridView1.Top + dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Y;


                seletime = ((DataGridViewTextBoxCell)dataGridView1.Rows[e.RowIndex].Cells["Column2"]);


                if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null || dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "")
                {
                    dateTimePicker1.Value = Convert.ToDateTime(DateTime.Now.ToString("HH:mm"));
                }
                else
                {
                    dateTimePicker1.Value = Convert.ToDateTime(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
                }
                dateTimePicker1.Visible = true;
            }
            else
            {
                dateTimePicker1.Visible = false;
            }
        }
    }
}

展开阅读全文

没有更多推荐了,返回首页