Jordan-C
Jordan-C

Reputation: 123

Restrict dates in 'datetimepicker' in C#

A feature of a forms based application I am developing allows the user to search through a history of records.

The user can search by name, by number, and between dates, and populate the results in a datagridview control.

Here is the form:

However, as the form will be used to search for previous records. The ability for the user to select future dates is not required.

Is there a way to prevent the user from selecting future dates, or even grey the future dates out?

Upvotes: 2

Views: 12819

Answers (4)

Scott Earle
Scott Earle

Reputation: 680

Setting the MaxDate property will hide all dates after the specified date, and will not allow dates to be entered that are greater than that date.

See http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.maxdate.aspx for details.

Upvotes: 1

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

You can try with code based on MaxDate property

yourControl.MaxDate = DateTime.Today;

Link : http://msdn.microsoft.com/fr-fr/library/system.windows.forms.datetimepicker.maxdate.aspx

Upvotes: 8

Nasreddine
Nasreddine

Reputation: 37908

You can use the DateTimePicker.MaxDate Property:

DateTimePicker.MaxDate Property: Gets or sets the maximum date and time that can be selected in the control.

Upvotes: 3

petro.sidlovskyy
petro.sidlovskyy

Reputation: 5103

Please try:

dateTimePicker.MaxDate = DateTime.Now;

Upvotes: 3

Related Questions