|
-
Oct 9th, 2009, 09:29 AM
#1
Thread Starter
Addicted Member
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.
-
Oct 9th, 2009, 09:47 AM
#2
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
-
Oct 9th, 2009, 09:47 AM
#3
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:
Me.ToolStripDateTimePicker1.Value = Date.Today.AddMonths(-2) Me.ToolStripDateTimePicker2.Value = Date.Today
-
Oct 9th, 2009, 10:25 AM
#4
Thread Starter
Addicted Member
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.
-
Oct 9th, 2009, 10:35 AM
#5
Re: display default date on DateTimePicker
Yes just handle the ValueChanged events of each DateTimePicker and set the values accordingly.
-
Oct 9th, 2009, 10:55 AM
#6
Thread Starter
Addicted Member
Re: display default date on DateTimePicker
Thanks you very mutch. I got it.
-
Oct 9th, 2009, 07:42 PM
#7
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.
-
Oct 13th, 2009, 09:49 AM
#8
Thread Starter
Addicted Member
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.
-
Oct 13th, 2009, 10:23 AM
#9
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?
-
Oct 13th, 2009, 10:52 AM
#10
Thread Starter
Addicted Member
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)
-
Oct 13th, 2009, 10:58 AM
#11
Fanatic Member
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
-
Oct 13th, 2009, 11:30 AM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|