Do not set default value

 Xamarin.Forms  -> Picker -> Datetimepicker

Hello, when using Datetimepicker, if the date is DBNull, how can I set the default value to the current date. Thank you very much!


3 Replies 1 reply marked as answer

SP Sakthivel Palaniyappan Syncfusion Team October 2, 2020 10:32 AM UTC

Hi Son,

Greetings from Syncfusion.

We have analyzed your query and we can achieve your requirement by setting SelectedItem to the SfPicker as like below code  snippet.

ViewModel:

 
  public class DateTimeViewModel : INotifyPropertyChanged 
    { 
        private ObservableCollection<object> _startdate; 
 
        public ObservableCollection<object> StartDate 
        { 
            get { return _startdate; } 
            set { _startdate = value; RaisePropertyChanged("StartDate"); } 
        } 
 
        public DateTimeViewModel() 
        { 
            ObservableCollection<object> todaycollection = new ObservableCollection<object>(); 
 
            //Select today dates 
            todaycollection.Add(DateTime.Now.Date.Year.ToString()); 
 
            todaycollection.Add(CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(DateTime.Now.Date.Month).Substring(0, 3)); 
            if (DateTime.Now.Date.Day < 10) 
                todaycollection.Add("0" + DateTime.Now.Date.Day); 
            else 
                todaycollection.Add(DateTime.Now.Date.Day.ToString()); 
 
            if (DateTime.Now.Hour < 10) 
                todaycollection.Add("0" + DateTime.Now.Hour.ToString()); 
            else 
                todaycollection.Add(DateTime.Now.Hour.ToString()); 
 
            if (DateTime.Now.Minute < 10) 
                todaycollection.Add("0" + DateTime.Now.Minute.ToString()); 
            else 
                todaycollection.Add(DateTime.Now.Minute.ToString()); 
 
            this.StartDate = todaycollection; 
        } 
    } 

XAML:

 
  <local:DateTimePicker 
            x:Name="date" 
            ColumnHeaderHeight="40" 
            HorizontalOptions="Center" 
            PickerHeight="400" 
            PickerMode="Dialog" 
            PickerWidth="300" 
            SelectedItem="{Binding StartDate}" 
            VerticalOptions="Center"> 

We have created sample based on your requirement and please find the sample from below.

Sample:
https://www.syncfusion.com/downloads/support/directtrac/general/ze/DateTimePicker1751972043.zip

Please let us know if you have any other queries.

Regards,
Sakthivel P.
 


Marked as answer

SH Son Hoang October 6, 2020 02:37 AM UTC

Thank kiu so much Sakthivel P. <3


SP Sakthivel Palaniyappan Syncfusion Team October 6, 2020 05:12 AM UTC

Hi Son,

Thanks for the update. Please get back us if you need further assistance on this.

Regards,
Sakthivel P.


Loader.
Up arrow icon