如何创建timepicker列datagridview [英] How to create a timepicker column datagridview

查看:74
本文介绍了如何创建timepicker列datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想在c#中的datagridview中创建一个datetimepicker列,以便从用户那里获取出勤时间。



我创建这个类

Hi,

I want to create a datetimepicker column in datagridview in c# for get time from user for attendance purpose.

I am create this class

public class CalendarColumn : DataGridViewColumn
    {
    public CalendarColumn() : base(new CalendarCell())
    {
    }

    public override DataGridViewCell CellTemplate
    {
        get
        {
            return base.CellTemplate;
        }
        set
        {
          if (value != null && 
                !value.GetType().IsAssignableFrom(typeof(CalendarCell)))
            {
                throw new InvalidCastException("Must be a CalendarCell");
            }
            base.CellTemplate = value;
        }
    }
}

public class CalendarCell : DataGridViewTextBoxCell
{

    public CalendarCell()
        : base()
    {
     
    }

    public override void InitializeEditingControl(int rowIndex, object
        initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
    {
      
        base.InitializeEditingControl(rowIndex, initialFormattedValue,
            dataGridViewCellStyle);
        CalendarEditingControl ctl =
            DataGridView.EditingControl as CalendarEditingControl;
        if (this.Value == null)
        {
            ctl.Value = (DateTime)this.DefaultNewRowValue;
        }
       
    }
    public override Type EditType
    {
        get
        {
             return typeof(CalendarEditingControl);
        }
    }
    public override Type ValueType
    {
        get
        {
              return typeof(DateTime);
        }
    }
    public override object DefaultNewRowValue
    {
        get
        {
           
            return DateTime.Now;
        }
    }
}

class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
{
    DataGridView dataGridView;
    private bool valueChanged = false;
    int rowIndex;

    public CalendarEditingControl()
    {
        this.Format = DateTimePickerFormat.Time;
    }

     public object EditingControlFormattedValue
    {
        get
        {
            return this.Value.ToShortTimeString();
        }
        set
        {            
            if (value is String)
            {
                try
                {
                  
                    this.Value = DateTime.Parse((String) value);
                }
                catch
                {
                  
                    this.Value = DateTime.Now;
                }
            }
        }
    }

   
    public object GetEditingControlFormattedValue(
        DataGridViewDataErrorContexts context)
    {
        return EditingControlFormattedValue;
    }

   
    public void ApplyCellStyleToEditingControl(
        DataGridViewCellStyle dataGridViewCellStyle)
    {
        this.Font = dataGridViewCellStyle.Font;
        this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
        this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
        this.ShowCheckBox = true;
        this.ShowUpDown = true;
        
    }

    public int EditingControlRowIndex
    {
        get
        {
            return rowIndex;
        }
        set
        {
            rowIndex = value;
        }
    }

    public bool EditingControlWantsInputKey(
        Keys key, bool dataGridViewWantsInputKey)
    {
        switch (key & Keys.KeyCode)
        {
            case Keys.Left:
            case Keys.Up:
            case Keys.Down:
            case Keys.Right:
            case Keys.Home:
            case Keys.End:
            case Keys.PageDown:
            case Keys.PageUp:
                return true;
            default:
                return !dataGridViewWantsInputKey;
        }
    }

    public void PrepareEditingControlForEdit(bool selectAll)
    {
     }
 
    public bool RepositionEditingControlOnValueChange
    {
        get
        {
            return false;
        }
    }

     public DataGridView EditingControlDataGridView
    {
        get
        {
            return dataGridView;
        }
        set
        {
            dataGridView = value;
        }
    }

     public bool EditingControlValueChanged
    {
        get
        {
            return valueChanged;
        }
        set
        {
            valueChanged = value;
        }
    }

     public Cursor EditingPanelCursor
    {
        get
        {
            return base.Cursor;
        }
    }

    protected override void OnValueChanged(EventArgs eventargs)
    {
          valueChanged = true;
        this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
        base.OnValueChanged(eventargs);
    }
}





我想查看或选择时间。上面的代码运行良好。但是当我改变

它显示的时间与日期和时间。我想只显示时间。怎么做。请有人帮忙吗?



I want to view or select time only. Above code is working well. But when I change
the time it displays with date and time. I want to display only time. How to do it. Please anyone can help me?

推荐答案

您可以通过添加自定义格式字符串来使用内置的DateTime选择器,如下所示:

You can use the built in DateTime picker by adding a custom format string as below:
DateTimePicker1.ShowUpDown = true;
DateTimePicker1.CustomFormat = "hh:mm";//You can put the desired format you need in here
DateTimePicker1.Format = DateTimePickerFormat.Custom;







如果您想保存,请从中取出值并插入。



还有一件事,请你的英语也很好。有点难以理解你的写作。

祝你好运。



And
If you want to save, take the value out of it and Insert it.

One more thing, Please work on your english also.Its kinda hard to understand what you write.
Good Luck.


这篇关于如何创建timepicker列datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆