How to stop DateTimePicker from showing Todays Date - vb.net

Everytime when I start my program DateTimePicker automatically shows todays date and time. How can I stop this ? How can I make it blank ?
I have a DOB DateTimePicker. For some users I don't know their DOB so I would like the DateTimePicker to show null or empty field.

Set DateTimePicker.CustomFormat = " " Make sure there is a space. This will show up as a blank space.
I used a DataRowView drv = DBBindingSource(DB). along with a control statement to set null when needed.
if(drv.Row["DOB"] == DBNull.Value)
{
DateTimePicker.CustomFormat = " ";
}
else
{
DateTimePicker.CustomFormat = "dd MMMM yyyy";
}

I don't think there is a way to allow it to be blank but you could use the ShowCheckBox and Checked properties set to true. The DateTimePicker will then have a checkbox in addition to the date dropdown. The date dropdown is disabled when not checked. This allows you to have a 'no value' or null for the DOB when the checkbox is not checked.

See this CodeProject article on a Nullable DateTimePicker. It inhertis from DateTimePicker.

Use this :
Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
DateTimePicker1.Value = DateTimePicker1.Value.Today
End Sub

Set it's Value property to the date you want.

if you are talking about datetimepicker from Trent Richardson, have a look at this thread
https://stackoverflow.com/a/12645192/551811
basically, set the inputbox value in the onClose event.
$('input.datetime').datetimepicker({
onClose: function (value) {
$('input.datetime').val(value);
}
});

my solution:
Public Sub ClearDatePicker(ByVal DatePicker As DateTimePicker)
DatePicker.CustomFormat = " "
DatePicker.Format = DateTimePickerFormat.Custom
End Sub
then the datetimepicker will be displayed empty.

Related

Adding a year to date in vb.net

I have two DateTimePicker controls on a form.
How can I automatically add a year to the date I have selected in the 1st DateTimePicker and display it on the 2nd DateTimePicker? For example, if select a date 09/22/2017, then I want the 2nd DateTimePicker to display 09/22/2018.
Is this possible? If so, would you suggest some appropriate code? If not, would you suggest an alternative way to do it?
You can do it like this, using the DateTimePicker's ValueChanged event:
Private Sub DateTimePicker1_ValueChanged(Sender As DateTimePicker, e As EventArgs) Handles DateTimePicker1.ValueChanged
Me.DateTimePicker2.Value = Sender.Value.AddYears(1)
End Sub
To automatically generate this event handler code, double-click the first DateTimePicker control on your form.
You'll notice in this example that I've changed the casting of Sender from Object to DateTimePicker, so we can get IntelliSense in the IDE.
Not sure what exactly you need to do and what YEAR you're looking for, but the simple answer is:
SomeComboBox.text = Now.Year
If you are selecting a date then take the year from the datepicker.
Append it to the textbox below.
You can do it in this way.
year = dtp1.Value.Year
tb.Text = tb.text + year
This will just append the year when you change the datepicker however if you want tb.Text year to be replaced on every click of datepicker then you should do something like this.
Dim str as String = tb.Text 'Save current text
Dim arr as String() = str.Split(",")'Get previous text
currentYear = dtp1.Value.Year 'Get current year
tb.Text = arr(0) + currentYear 'Replace year in textbox with new year

How to set null value to Datetimepicker when load?

I have a Datetimepicker1, i want to set it to empty when load, and show value after chose.
I have found a code
Datetimepicker1.Format = DateTimePickerFormat.Custom
Datetimepicker1.CustomFormat = " "
but it does not show value after chose.
Try this
DateTime? dateSample= null;
DateTimePickers can't be set to null because DateTime can't be null, but you can set them to DateTime.MinValue which is the default value for an uninitialized DateTime. And then you just check if the dtp.Value = DateTime.MinValue and if so, treat it as null.
However, if you want to really distinguish when no value has been selected, the easiest way is to set DateTimePicker.ShowCheckBox to true, and then you check dtp.Checked and if it's true, you read the value, otherwise you treat it as a null.
Have you try this : for testing purpose
DateTime? myselectedDate = null;
private void DateTimePicker1_ValueChanged(Object sender, EventArgs e) {
myselectedDate = DateTimePicker1.Value;
}
And
this.dateTimePicker1.Format = DateTimePickerFormat.Custom;
this.dateTimePicker1.CustomFormat = " "; //a string with one whitespace
DatetimePicker can't have no value. Null or default value is DateTime.MinValue. Your code changes the format in order to show nothing.
To show the date again, simply change DateTimePicker.Format again when needed:
DateTimePicker1.Format = DateTimePickerFormat.Long;
Because , you formatted your datetimepicker "", you should write the below code in valuechanged event of the datetimepicker;
Datetimepicker1.Format = DateTimePickerFormat.short(type which you need short,long etc..)

Show calendar in Ultragrid Column Filter

I have an UltraGrid in which I have many columns of which 2 columns are DateTime style. Now when I use the filter of that columns it shows all the DateTime values as a text in a dropdown. But I need that as a calendar in order to make the filter easy. It is enough to show just a calendar when clicking the filter.
I have tried some code but it doesn't work.
//Code:
Private Sub grdResult_BeforeRowFilterDropDown(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.BeforeRowFilterDropDownEventArgs) Handles grdResult.BeforeRowFilterDropDown
e.Cancel = True
UltraCalendarCombo1.Visible = True
UltraCalendarCombo1.Location = New Point(grdResult.Rows.FilterRow.Cells(e.Column).GetUIElement().Rect.Location.X, grdResult.Rows.FilterRow.Cells(e.Column).GetUIElement().Rect.Location.Y - 2)
UltraCalendarCombo1.Size = New System.Drawing.Size(grdResult.Rows.FilterRow.Cells(e.Column).GetUIElement().Rect.Size.Width, grdResult.Rows.FilterRow.Cells(e.Column).GetUIElement().Rect.Size.Height)
' UltraCalendarCombo1.DroppedDown = True
End Sub
The above event will fire when the filter dropdown is clicked.
private sub applyCustomeViewSettings(byval gridFormat as GridFormat)
....
...
For Each ColumnFormat In gridFormat.ColumnFormats
For Each column In Me.grdResult.DisplayLayout.Bands(0).Columns
If column.Key.ToUpper = ColumnFormat.ColumnKey.ToUpper Then
If column.Key.ToUpper = "PCSSTDT" Then
column.Header.Caption = IIf(ColumnFormat.Caption = "", ColumnFormat.ColumnKey, ColumnFormat.Caption)
column.Hidden = ColumnFormat.Hidden
'column.AllowRowFiltering = IIf(ColumnFormat.AllowRowFiltering = False, ColumnFormat.AllowRowFiltering, DefaultableBoolean.True) 'CType(ColumnFormat.AllowRowFiltering, DefaultableBoolean)
column.Width = ColumnFormat.Width
column.Header.VisiblePosition = ColumnFormat.VisiblePosition
column.Format = ColumnFormat.Format
column.SortIndicator = ColumnFormat.SortIndicator
' column.Style = ColumnStyle.Date
'column.EditorComponent = UltraCalendarCombo1
column.FilterOperandStyle = FilterOperandStyle.Default
Else
column.Header.Caption = IIf(ColumnFormat.Caption = "", ColumnFormat.ColumnKey, ColumnFormat.Caption)
column.Hidden = ColumnFormat.Hidden
column.AllowRowFiltering = IIf(ColumnFormat.AllowRowFiltering = False, ColumnFormat.AllowRowFiltering, DefaultableBoolean.True) 'CType(ColumnFormat.AllowRowFiltering, DefaultableBoolean)
column.Width = ColumnFormat.Width
column.Header.VisiblePosition = ColumnFormat.VisiblePosition
column.Format = ColumnFormat.Format
column.SortIndicator = ColumnFormat.SortIndicator
column.Style = ColumnFormat.Style
End If
End If
Next
....
...
End Sub
The above method makes the grid changes(apply settings) to show the filter as calendar.
But this doesn't work and showing me the same normal grid.
How can I achieve this?
I have managed to show a MonthCalendar when you press the icon to filter a DateTime column in an UltraWinGrid.
When the MonthCalendar is dispayed you could select a specific date using the familiar interface provided by this standard WinForm control. After selecting the date you could use the value to apply programmatically a filter condition to the UltraWinGrid column.
To reach this result you first need to add a reference to the Infragistics4.Win.SupportsDialog.v11.2 assembly where you can find the UltraGridFilterUIProvider class
Now, in your form where you need the filtering to appear, add this code: (it is just an example because I haven't your datasource and thus I have a prebuilt one with just one datetime column)
Imports Infragistics.Win.UltraWinGrid
Imports Infragistics.Win.SupportDialogs.FilterUIProvider
Public Class Form1
' This is the key object that let us customize '
' the Filter for the UltraWinGrid'
Dim _filterUIProvider as UltraGridFilterUIProvider
' In the InitializeLayout event we substitute the normal
' filter handler with the custom filter'
Private Sub UltraGrid1_InitializeLayout(sender As Object, e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) Handles UltraGrid1.InitializeLayout
e.Layout.Override.AllowRowFiltering = Infragistics.Win.DefaultableBoolean.True
_filterUIProvider = New UltraGridFilterUIProvider()
' Comment out the following line to test the default
' **Excel Filter Style Interface** '
AddHandler _filterUIProvider.BeforeMenuPopulate, AddressOf _filterUIProvider_BeforeMenuPopulate
e.Layout.Override.FilterUIProvider = _filterUIProvider
End Sub
' Before the UltraGridFilterUIProvider shows its standard form interface
' we start a custom form used to apply our filtering logic '
' and block the display of the UltraGridFilterUIProvider interface '
Private Sub _filterUIProvider_BeforeMenuPopulate(sender As Object, e As Infragistics.Win.SupportDialogs.FilterUIProvider.BeforeMenuPopulateEventArgs)
' A custom form with the MonthCalendar and 3 buttons '
' to handle the filter logic '
Using fDate = new FormDate()
' Open our custom form with the monthcalendar
if (DialogResult.OK = fDate.ShowDialog())
' We need a nullable date to allow the removing of the filter'
Dim dtFilter As DateTime? = fDate.SelectedDate
if (dtFilter.HasValue)
' Apply programmatically a filtercondition to the column
' In this case I have only one column. so I use the index 0
' in your case this should change to reflect your column index
Dim fc = new FilterCondition(FilterComparisionOperator.Equals, dtFilter.Value)
ultraGrid1.DisplayLayout.Bands(0).ColumnFilters(0).FilterConditions.Add(fc)
Else
ultraGrid1.DisplayLayout.Bands(0).ColumnFilters.ClearAllFilters()
End If
End If
End Using
e.Handled = true ' Stop the standard interface'
End Sub
End Class
Now we need only a simple form called FormDate that contains a MonthCalendar and three buttons (Set, Clear, Cancel) that return (Set) a Date to set the filter, (Clear) a null value to remove previous filter and a cancel button to abort the processing
Here the code for the form, the design is trivial
Public Class FormDate
Public Property SelectedDate As DateTime?
Private Sub FormDate_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.SelectedDate = Nothing
End Sub
Private Sub cmdSet_Click(sender As Object, e As EventArgs) Handles cmdSet.Click
'This button has DialogResult=OK'
Me.SelectedDate = monthCalendar1.SelectionStart
End Sub
Private Sub cmdClear_Click(sender As Object, e As EventArgs) Handles cmdClear.Click
'This button has DialogResult=OK'
Me.SelectedDate = Nothing
End Sub
End Class
This could resolve your problem, however, I have discoverd what seems to be a bug in UltraGridFilterUIProvider. When I call the e.Handled=True my expected result is the filter to not show anything but, instead a small window still appears and I have to press Escape to hide it. I have not found any way to automatically hide it.
It seems to be a problem to signal to the Infragistics team.
I suggest you also to test the Excel Style Filter Interface provided automatically by the UltraGridFilterUIProvider. This interface has a lot of options and is much more preferable to the standard UI filter. To test this interface you should only comment out the AddHandler line above
EDIT Following the comment from #Alhalama I have tried to use the BeforeRowFilterDropDown event and the result are better (well it is perfect now). So I have commented out the line with the AddHandler, removed the code for the BeforeMenuPopulate and added the code for the BeforeRowFilterDropDown
Private Sub UltraGrid1_BeforeRowFilterDropDown(sender As Object, e As BeforeRowFilterDropDownEventArgs) Handles UltraGrid1.BeforeRowFilterDropDown
If e.Column.Key = "DateRequest" Then
Using fDate = New FormDate()
If DialogResult.OK = fDate.ShowDialog() Then
Dim dtFilter As DateTime? = fDate.SelectedDate
If (dtFilter.HasValue) Then
Dim fc As FilterCondition = New FilterCondition(FilterComparisionOperator.Equals, dtFilter.Value)
UltraGrid1.DisplayLayout.Bands(0).ColumnFilters(0).FilterConditions.Add(fc)
Else
UltraGrid1.DisplayLayout.Bands(0).ColumnFilters.ClearAllFilters()
End If
End If
End Using
e.Cancel = True
End If
End Sub
Now, when I try to open the filter for the column named DateRequest I open immediately the FormDate and at the end I set the Cancel property of the BeforeRowFilterDropDownEventArgs to true to avoid further processing of the filter dialog. This seems to be perfect...... Great credit for this to Mr. Alhalama. If you wish I think you should post your own answer because your suggestion really makes the difference.

Why does DataGridViewComboBoxColumn display a ValueMember?

I have a small problem. DataGridViewComboBoxColumn displays a value from ValueMember and not from DisplayMember. The grid works fine and when I select something from this column I see the DisplayMember value, but when the focus gets lost, the grid shows a ValueMember. I have this code combo box column:
statusCBoxColumn.DataSource = dt 'datatable with two fields StatusId and StatusText
statusCBoxColumn.DisplayMember = "StatusText" 'is type NVarchar
statusCBoxColumn.ValueMember = "StatusId" 'is type Int
Can anybody help me?
Edit: I solved this in the following way:
Private Sub dgv_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgv.CellFormatting
If (dgv.Columns(e.ColumnIndex).Name = "statusCBoxColumn") Then
If e.Value & "" > "" Then
Dim s1 As String = e.Value
e.Value = GetData("Select StatusText from Status where ID = " & e.Value).ToString()
End If
End If
End Sub
But I don't think that it's the best solution...
Have you set datapropertyName of the comboboxcolumn and added the column to datagridview? You've to write
statusCBoxColumn.DataPropertyName = "StatusId";
and add the column like
datagridview1.Columns.Add(statusCBoxColumn);
I had the same problem.
The "statusID" for me is long.
I made it by system.type.gettype("System.Int64")
That gave the same behaviour.
When changing it to Int32, the behaviour was OK.
So it was à mismatch between valuemember type and datagridview type.
I know this is a 10 years old question, but I was having same problem and I found out that according to MSDN :
When the DataSource property is set to a string array, the ValueMember property does not need to be set because each string in the array will be used as a valid display string and as a valid underlying value.
Try this one I've also have a problem on displaying value member,hope it will work.
For index = 0 To cbo.Items.Count - 1
cbo.SelectedIndex = index
MessageBox.Show(CType(cbo.SelectedValue, String))
Next
What i had used is:
Dim jm As Integer
jm = CType(cmbCategory.SelectedValue, String)
MsgBox(jm)
e.Value = GetData("Select to_char(StatusId) , StatusText from Status where ID = " & e.Value).ToString()

InvalidCastException error with clearing value of date picker

I use code such as this to clear out the values in several text boxes:
Private Sub RibbonButtonInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RibbonButtonInsert.Click
UpdateDataSource()
StudentsBindingSource.ResetBindings(True)
DatePickerDateOfBirth.Value = ""
RichEditBoxForeName.Focus()
StudentsBindingSource.AddNew()
End Sub
Because the date picker control did not clear out like the other controls did, I tried:
DatePickerDateOfBirth.Value = ""
but it does not clear out. Instead, I get an "InvalidCastException".
Can you show me what coding I need to properly clear out the value in this data picker?
Actually you are not clearing it out, you are assigning a string value to it. and as it is a Date type, the assignment is wrong.
the assigned value must be a Date format, you can do this:
DatePickerDateOfBirth.Value = Date.MinValue
later you can check and if the value is equal to MinValue, then the date is not set.
You cant set DateTimePicker Value to null or empty string it's value must be a date
but you can but a checkbox in datetimepicker using ShowCheckBox Property
and then use dateTimePicker.Checked = false dateTimePicker.Checked