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
close icon

Min and Max date for MonthCalendar cell

I''m using some MonthCalendar controls in a GridControl. grid1[4,3].CellType = "MonthCalendar"; grid1[4,3].CellValue = DateTime.Now; grid1[4,3].Format = "MM/dd/yyyy"; Is there a way to set the min/max dates allowable for the month calendar drop down? Thanks!

7 Replies

AD Administrator Syncfusion Team September 10, 2004 09:42 PM UTC

You can use CurrentCellShowingDropDown and set the values there dynamically. You do have to hunt down the calendar control as it is not exposed as a public member of the renderer class.
private void gridControl1_CurrentCellShowingDropDown(object sender, GridCurrentCellShowingDropDownEventArgs e)
	GridCurrentCell cc = this.gridControl1.CurrentCell;
	GridDropDownMonthCalendarCellRenderer cr = cc.Renderer as GridDropDownMonthCalendarCellRenderer;
	if(cr != null)
		MonthCalendar cal = null;
		foreach(Control c in cr.DropDownContainer.Controls)
			if(c is MonthCalendar)
				cal = c as MonthCalendar;
		if(cal != null)
			cal.MaxDate = DateTime.Now.AddDays(100);
			cal.MinDate = DateTime.Now.AddDays(-100);

DA Dan September 13, 2004 10:17 PM UTC

Is there any other way to get at the calendar control before it''s dropped down by the user? The text field portion of the control needs to be in a certain format (MMM dd, yyyy) and displayed like that with default values when the form loads - before the user is going to start clicking on the controls. Thanks.

AD Administrator Syncfusion Team September 14, 2004 05:32 AM UTC

To use a custom date, you can use the style.Format property to set the format. GridStyleInfo style = this.gridControl1[1,2]; style.CellType = "MonthCalendar"; style.CellValueType = typeof(DateTime); style.Format = "dd/MM/yy HH.mm.ss"; style.CellValue = DateTime.Now; But if you specify the format that is not consistent with the regional settings for dates, this will cause the grid to throw an exception as you try to drop the calendar. This is not a problem if you are not allowing editing, or if the format is consistent with the regional setting. But if you do run into the problem, one way to work around it is to derive the cell control and set the value into the calendar dropdown taking into account the format provided in the style. Here is a sample. This sample has both the custom celltype that handles the format, as well as several grid events that can also do the same thing. To see the grid events work, add this code to set one of the cells to use the standard MonthCalendar. //uses events to set a format style = this.gridControl1[5,2]; style.CellType = "MonthCalendar"; style.CellValueType = typeof(DateTime); style.Format = "MM/dd/yy HH.mm.ss"; style.BackColor = Color.LightGoldenrodYellow; style.CellValue = DateTime.Now;

DA Dan September 14, 2004 03:26 PM UTC

Thanks Clay. I''m trying to run the sample you provided, and get build errors around CalendarControlSize GridCapture HScrollPixel ...etc... I suspect you''re using a newer grid vesion. I''m on I''ll have to look into deriving the cell control to handle different culture settings. One more question :) Is there a way to make the edit portion of the month calendar control read-only? I don''t want to allow users to type in the date, only select it from the drop-down. Thanks.

AD Administrator Syncfusion Team September 14, 2004 03:52 PM UTC

Try just commenting out anything that causes a syntax error. Try handling the CurrentCellStartEditing event. In your handler, set e.Cancel = true if grid.CurrentCell.RowIndex/ColIndex point to a cell on which you do not want to allow editing.

DA Dan September 14, 2004 04:05 PM UTC

Thanks - setting e.Cancel = true works, prevents editing in the text portion of the calendar control, BUT has the following side effect: When I drop down the calendar control, and choose a date, that date is not placed into the edit box.

AD Administrator Syncfusion Team September 14, 2004 04:39 PM UTC

Here is a sample that does this using CurrentCellKeyPress event.

Live Chat Icon For mobile
Up arrow icon