We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Time Picker

I need to display cells that contain (and allow validated editing of) the time only, in AM/PM format. The DropDownMonthCalendar does not appear to have facilities for picking the time, and I do not want or need to modify the date. The .Net Forms.DateTimePicker manages to do this job ok if you disable the dropdown calendar, set the format to time, and turn on the up/down buttons. However, I'm not sure how (or if) I can use this in the grid? Another option appears to be using a standard text cell that I provide a format string for (from my DateTime object) and then manually validate the input during SaveCellInfo. Before implementing either of these options, I figured I should ask here in case anyone has solved this problem already. It seems like it would be a fairly common need... thanks for any suggestions and/or sample code. -dan

1 Reply

AD Administrator Syncfusion Team June 10, 2003 07:39 AM UTC

You can just use an TextBox cell with this code:
//the initialization
GridStyleInfo style = this.gridControl1[2,2];
style.CellValueType = typeof(DateTime);
style.Format = "hh:mm tt";
style.CellValue = DateTime.Now;

//handle CurrentCellValidating to enforce teh format to your needs.
private void gridControl1_CurrentCellValidating(object sender, System.ComponentModel.CancelEventArgs e)
{
	GridCurrentCell cc = this.gridControl1.CurrentCell;
	GridStyleInfo style = this.gridControl1[cc.RowIndex, cc.ColIndex];
	if(style.CellValueType == typeof(DateTime) && style.Format.Length > 0)
	{
		string[] validFormats = new string[]{style.Format, "h:mm tt", "h:m tt", "hh:m tt"};
		string s = cc.Renderer.ControlText;
		try
		{
			DateTime dt = DateTime.ParseExact(s, validFormats, null, 
                             DateTimeStyles.AllowInnerWhite|DateTimeStyles.AllowLeadingWhite
                             |DateTimeStyles.AllowTrailingWhite|DateTimeStyles.AllowWhiteSpaces);
			string s1 = dt.ToString(style.Format);
			if(s1 != s)
			{
				cc.Renderer.Control.Text = s1;
			}
		}
		catch
		{
			cc.ErrorMessage = "Invalid Time";
			e.Cancel = true;
		}
	}
}
In 1.6, there is actually a Control celltype you can try to use to put a DateTimePicker in a cell. Below is some code. But it really is only useful for a static type control that does not change sizes an donly lives in a single cell. If you want a more versatile DateTimePicker in a cell, then you should use the technique illustrated in the Syncfusion\Essential Suite\Grid\Samples\CellTypes\CalendarCells sample in 1.6. It creates a MonthCalender cell that can be shared and sized among different grid cells bases on the Windows Forms DateCalendar control.
DateTimePicker dtp = new DateTimePicker();
dtp.AllowDrop = false;
dtp.ShowUpDown = true;
dtp.Format = DateTimePickerFormat.Custom;
dtp.CustomFormat = "hh:mm tt";
			
style = this.gridControl1[4,2];
style.CellValueType = typeof(DateTime);
style.Format = "hh:mm tt";
style.CellValue = DateTime.Now;
style.CellType = "Control";
style.Control = dtp;

Loader.
Up arrow icon