i have problem in date time picker actually i m putting validation for cheque date .. it should not be greater than 180 days . because cheque id valid for 180 days so user only can put date from today dates to 180 days .. is it pssoible in c#.. if yes than let me know.. this is my code please check it ....
if (dtp_cheque_date.Value < DateTime.Today)
{
MessageBox.Show("Cheque Date cannot be less than today!");
dtp_cheque_date.Value = DateTime.Today;
}
thanks in Advance

Nilanka DharmadasaPosted Dec 22, 2009, 6:22 AM
In the closeup event of datetime picker you can write the following code.
private void dtp_cheque_date_CloseUp(object sender, EventArgs e)
{
if (dtp_cheque_date.Value < DateTime.Today)
{
MessageBox.Show("Cheque Date cannot be less than today!");
dtp_cheque_date.Value = DateTime.Today;
return;
}
if (dtp_cheque_date.Value > (DateTime.Today.AddDays(180)))
{
MessageBox.Show("Cheque Date cannot be greater than 180 days from today!");
dtp_cheque_date.Value = DateTime.Today;
}
}
Or you can set the maxdate of datetimepicker. But it will not display any error message to the user.
dtp_cheque_date.MaxDate = DateTime.Today.AddDays(180);
If you find my answer useful, please tick 'Do you like this answer' checkbox.
Hiren SoniPosted Dec 22, 2009, 6:52 AM