Time Column For DataGridView



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace Mesoft.Control
{
    using System.Windows.Forms;
    public class TimeColumn : DataGridViewColumn
    {
        public TimeColumn()
            : base(new TimeCell())
        { 
        }
        public TimeColumn(string frm)
            : base(new TimeCell(frm))
        {
        }
        public void setValue(string frm)
        {
            base.CellTemplate.Value = frm;
        }
        [Category("Mesoft")]
        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                if (value != null && !value.GetType().IsAssignableFrom(typeof(TimeCell)))
                {
                    throw new InvalidCastException("Must be a TimeCell");
                }
                base.CellTemplate = value;
            }
        }
    }
    public class TimeCell : DataGridViewTextBoxCell
    {
        public TimeCell()
            : base()
        {
            this.Style.Format = "hh:mm tt";
        }
        public TimeCell(string frm)
            : base()
        {
            this.Style.Format = frm;
        }
        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            try
            {
                base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
                TimeEditingControl ctl = DataGridView.EditingControl as TimeEditingControl;
                ctl.Format = DateTimePickerFormat.Time;
                ctl.ShowUpDown = true;
                ctl.Value = (DateTime)this.Value;
            }
            catch { }
        }
        [Category("Mesoft")]
        public override Type EditType
        {
            get
            {
                return typeof(TimeEditingControl);
            }
        }
        [Category("Mesoft")]
        public override Type ValueType
        {
            get
            {
                return typeof(DateTime);
            }
        }
        [Category("Mesoft")]
        public override object DefaultNewRowValue
        {
            get
            {
                return DateTime.Now;
            }
        }
    }
    class TimeEditingControl : DateTimePicker, IDataGridViewEditingControl
    {
        DataGridView dataGridView;
        private bool valueChanged = false;
        int rowIndex;
        public TimeEditingControl()
        {
            this.Format = DateTimePickerFormat.Custom;
        }
        [Category("Mesoft")]
        public object EditingControlFormattedValue
        {
            get
            {
                return this.Value.ToShortTimeString();
            }
            set
            {
                String newValue = value as String;
                if (newValue != null)
                {
                    this.Value = DateTime.Parse(newValue);
                }
            }
        }
        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            return EditingControlFormattedValue;
        }
        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = dataGridViewCellStyle.Font;
        }
        [Category("Mesoft")]
        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:
                case Keys.Tab:
                    return true;
                default:
                    return false;
            }
        }
        public void PrepareEditingControlForEdit(bool selectAll){}
        [Category("Mesoft")]
        public bool RepositionEditingControlOnValueChange
        {
            get
            {
                return false;
            }
        }
        [Category("Mesoft")]
        public DataGridView EditingControlDataGridView
        {
            get
            {
               return dataGridView;
            }
            set
            {
                dataGridView = value;
           }
        }
        [Category("Mesoft")]
       public bool EditingControlValueChanged
       {
            get
            {
                return valueChanged;
            }
            set
            {
                valueChanged = value;
            }
        }
        [Category("Mesoft")]
        public Cursor EditingPanelCursor
        {
            get
            {
                return base.Cursor;
            }
        }
        protected override void OnValueChanged(EventArgs eventargs)
        {
            valueChanged = true;
            this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
            base.OnValueChanged(eventargs);
        }
    }
}

No comments: