winform,DataGridView单元格点击选择日期,日期控件

winform,DataGridView单元格点击选择日期,日期控件

在这里插入图片描述

/// <summary>
/// 新增,工作日历配置
/// </summary>
public partial class FrmCalendarProductivityAdd : BaseForm.FrmBaseForm
{
    public static string strUserId, strUserName, personId, personName;
    public SqlConnection myConn;


    /// <summary>
    /// 时间控件
    /// </summary>
    private DateTimePicker _dtptime = new DateTimePicker();


    BasCommand.BasCommand bs = new BasCommand.BasCommand();
 

    public FrmCalendarProductivityAdd(FrmWorkingCalendar _parentFrm)
    {
        InitializeComponent(); 
    }
    
    private void FrmCalendarProductivityAdd_Load(object sender, EventArgs e)
    {
        this.hlDataGridView1.AutoGenerateColumns = false;
       
        this.hlDataGridView1.Scroll += (s, e2) =>
        {
            //滚动条滚动时,隐藏时间控件
            _dtptime.Visible = false;
        };
        //编辑表格中的,检验日期,弹出时间控件
        this.Controls.Add(_dtptime);
        _dtptime.Visible = false;
        _dtptime.Font = new Font("宋体", 11F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134)));
        _dtptime.Format = DateTimePickerFormat.Custom;
        _dtptime.CustomFormat = "yyyy-MM-dd";

        //更新表格日期值,选择的日期复制给单元格
        _dtptime.CloseUp += (o, e45) =>
        {
            var dtp = o as DateTimePicker;
            DataGridViewCell currentCell3 = dtp.Tag as DataGridViewCell;
            if (currentCell3 != null)
            {
                currentCell3.Value = dtp.Value;
                currentCell3.DataGridView.EndEdit();                    
                dtp.Visible = false;
            }
        };
 
        this.hlDataGridView1.CellClick += HlDataGridView1_CellClick;
    }

    private void HlDataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        var dgv = sender as HLDataGridView;
        if (e.RowIndex < 0 || e.ColumnIndex < 0)
        {
            return;
        }

        //如果点击的是日期列,则显示日期控件
        if (dgv.Columns[e.ColumnIndex].Name == "date")
        {
            SelectedDate(dgv.CurrentCell);
        }
    }



    /// <summary>
    /// 点击时间单元格时,替换为日期控件
    /// </summary>
    /// <param name="currentCell"></param>
    private void SelectedDate(DataGridViewCell currentCell)
    {
        _dtptime.Tag = null;
        var dgv = currentCell.DataGridView;
        //获取单元格位置
        Rectangle _rect = this.RectangleToClient(dgv.RectangleToScreen(dgv.GetCellDisplayRectangle(currentCell.ColumnIndex, currentCell.RowIndex, true)));

        _dtptime.LostFocus += (o, e) =>
        {
            _dtptime.Visible = false;
        };
        _dtptime.Location = _rect.Location;
        _dtptime.Size = _rect.Size;
        _dtptime.BringToFront();

        _dtptime.Value = bs.GetDynamicToValue<DateTime>(currentCell.OwningRow.Cells["date"].Value, DateTime.Now.Date);
        _dtptime.Visible = true;
        _dtptime.BringToFront();
        _dtptime.Focus();    
        _dtptime.Tag = currentCell;
    }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王焜棟琦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值