Results 1 to 12 of 12

Thread: display default date on DateTimePicker

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    176

    display default date on DateTimePicker

    Hi All.
    I would like to create ToolStripe on my DGV form with 2 DateTimePicker tools. And when user open form those tools will display by default date:
    - first DateTimePicker will show date on 2 month early then current date;
    - second DateTimePicker will show current date.
    If it possible how to do that?
    Thanks.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: display default date on DateTimePicker

    All you really need to do is add your 2 datetimepicker controls to the form, and then in the form load, you need to assign them to the toolstrip. You can do that with code like this:

    Code:
    Public Class Form1
    
        Private _DateTimePickerHost1 As ToolStripControlHost
        Private _DateTimePickerHost2 As ToolStripControlHost
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            DateTimePicker1.Value = Now.AddMonths(-2)
            DateTimePicker2.Value = Now
    
            _DateTimePickerHost1 = New ToolStripControlHost(DateTimePicker1)
            _DateTimePickerHost2 = New ToolStripControlHost(DateTimePicker2)
    
            ToolStrip1.Items.Add(_DateTimePickerHost1)
            ToolStrip1.Items.Add(_DateTimePickerHost2)
    
        End Sub
    End Class
    This code assumes you have 2 datetimepicker controls on the form called DateTimePicker1 and DateTimePicker2

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: display default date on DateTimePicker

    The first thing you'll have to do is create a custom ToolStripControlHost to host a DateTimePicker. You can use the ToolStripTextBox and/or ToolStripComboBox classes as guides. They both inherit the ToolStripControlHost class and then host a specific control, i.e. TextBox and ComboBox. You can do the same for the DateTimePicker.

    Once you've defined the class you simply add two instances to your ToolStrip the same way you would any other item. You can then set their values in code, e.g.
    vb.net Code:
    1. Me.ToolStripDateTimePicker1.Value = Date.Today.AddMonths(-2)
    2. Me.ToolStripDateTimePicker2.Value = Date.Today
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    176

    Re: display default date on DateTimePicker

    Thanks a lot. That amassing. It looks exactly like I expected.
    Is it possible to modify it by this way. If user will change value on second DateTimePicker the value on first DateTimePicker automatically change on according 2 month.
    Or if user will change value on first DateTimePicker the value on second DateTimePicker automatically change on 2 month. So the user will have every time difference between DateTimePicker control 2 month where value 2nd DateTimePicker > 1st DateTimePicker on 2 month.
    Thanks.

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: display default date on DateTimePicker

    Yes just handle the ValueChanged events of each DateTimePicker and set the values accordingly.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    176

    Re: display default date on DateTimePicker

    Thanks you very mutch. I got it.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: display default date on DateTimePicker

    Just note that Matt's solution and mine both involve essentially the same thing, i.e. hosting a DateTimePicker control in a ToolStripControlHost. Matt's is quicker and is probably the better bet if this is a one-off and you don't mind not seeing the DateTimePickers at design time. My solution is a bit more work but provides you with design-time support and a component you can re-use in the future.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    176

    Re: display default date on DateTimePicker

    Hi All.
    Now I would like to filter my DGV based on values DateTimePicker1 and DateTimePicker2. How to do it?
    Thanks.

  9. #9
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: display default date on DateTimePicker

    It really depends on how you are linking up the data source to it. How are you going about that now?

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    176

    Re: display default date on DateTimePicker

    I'm binding DGV to TableAdapter based on SP
    Code:
    procedure [dbo].[sp_Appointments]
    (
    @AppointmentDateFrom as datetime
    ,@AppointmentDateTo as datetime
    )
    as
    
    select distinct
    a.Appointment_id
    ,FName
    ,LName
    ,convert(varchar(10),AppointmentDate,101) AppointmentDate
    Where convert(varchar(10),AppointmentDate,111) between convert(varchar(10),@AppointmentDateFrom,111) and convert(varchar(10),@AppointmentDateTo,111)

  11. #11
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    540

    Re: display default date on DateTimePicker

    nvm, thinking of something else...
    Last edited by BackWoodsCoder; Oct 13th, 2009 at 11:00 AM. Reason: oops wrong idea

  12. #12
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: display default date on DateTimePicker

    Probably the easiest way to do filters will be to set the filter property of the BindingSource that was created when you hooked up your data source to the datagridview.

    BindingSourceObjectName.Filter = "Your Filter Here"

    However it looks like your SP takes in a start and end date, and only selects the required records, so you may just need to reinvoke your SP each time, passing in the 2 dates you want to filter on, because just setting a filter property on the BindingSource is only going to filter the original dataset, which will be whatever data was retured from the SP when it was called.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width