Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows Application
i am using 2 datetimepicker.

1) any date select from first datetimepicker.

2) then another one date is select from second datetime picker.

my request is: the second datetimepicker selecting date only accept after first date.
how to compare from first date and second date.

EXAMPLE:

i.e) first selected date is 10/10/2011 ok.

then second datetimepicker selecting date accept only after first date i.e 11/10/2011

but can not accept before date that is 09/10/2011.
Posted 17-Oct-11 0:06am
Edited 17-Oct-11 0:11am
uspatel29.8K
v2
Comments
Uma Shankar Patel at 17-Oct-11 5:12am
   
Retaged from HTML to C#
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
lukeer at 17-Oct-11 5:25am
   
5 for ease of use.
Collin Jasnoch at 17-Oct-11 11:57am
   
+5 Simple and with MSDN reference :-)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Use Compare validater

<asp:CompareValidator ID="CompareValidator1" runat="server"  ControlToValidate="txtenddate"
ErrorMessage="End Date is not less than Start Date" Operator="GreaterThanEqual" Type="Date" Text="*" ControlToCompare="txtstartdate"></asp:CompareValidator>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

private void dateTimePicker1_ValueChanged(...)
{
dateTimePicker2.Value = DateTimePicker1.Value.AddDays(1);
}
 
private void dateTimePicker2_ValueChanged(...)
{
       if(dateTimePicker2.Value < DateTimePicker1.Value)
       {
         MessageBox.Show("Cannot be less than previous date");
         dateTimePicker2.Value = DateTimePicker1.Value.AddDays(1);
       }
}
 

Edit: Please Check my updated solution
  Permalink  
v3
Comments
lukeer at 17-Oct-11 5:26am
   
That wouldn't prevent User from selecting an earlier date on dateTimePicker2 afterwards.
Praveen Kullu at 17-Oct-11 5:32am
   
@Lukeer Thanks for pointing it out, I have updated the solution
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Or, you can try this: DateTime.Compare[^]

Nice and clean Smile | :) hope this helps

Cheers...
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 393
1 Sascha Lefévre 215
2 Abhinav S 170
3 Richard Deeming 155
4 CHill60 150
0 Sergey Alexandrovich Kryukov 7,141
1 OriginalGriff 6,410
2 Maciej Los 2,832
3 Abhinav S 2,732
4 Peter Leow 2,694


Advertise | Privacy | Mobile
Web01 | 2.8.150414.1 | Last Updated 17 Oct 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100