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 16-Oct-11 23:06pm
Edited 16-Oct-11 23:11pm
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 CPallini 350
1 Sergey Alexandrovich Kryukov 320
2 BillWoodruff 309
3 George Jonsson 274
4 OriginalGriff 237
0 OriginalGriff 4,903
1 CPallini 3,960
2 Sergey Alexandrovich Kryukov 3,474
3 George Jonsson 2,786
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web01 | 2.8.140905.1 | Last Updated 17 Oct 2011
Copyright © CodeProject, 1999-2014
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