Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted
Comments
Shanu2rick 12-Jan-13 2:12am    
you can take time out of the dateTimePicker's value.
OR
you just want to display time?
devausha 12-Jan-13 2:16am    
I want to allow to user select the time or enter the time use updown buttons
and then I get the time from datagridview to save

1 solution

You can use the built in DateTime picker by adding a custom format string as below:
C#
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.
 
Share this answer
 
Comments
devausha 12-Jan-13 2:44am    
I already give the above code to my class. My problem is I can changed the datagridview cell value into time , It display proper time which I have choosed when cursor is in the same cell. If cursor moves to another cell the previous cell value changed into date and time. But I want display only time.
Shanu2rick 12-Jan-13 3:29am    
Are you sure you have included this properties to your dataGridView Column for the DateTimePicker?
devausha 17-Jan-13 0:27am    
Yes, I am create the class calendarcolumn mentioned the questions
Shanu2rick 17-Jan-13 1:54am    
Check for any event firing when your cursor moves from one cell to another.
Debug the code, put a breakpoint and check!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900