Change datetime editor in RadGridView to inherited control

2 posts, 0 answers
  1. Patryk
    Patryk avatar
    6 posts
    Member since:
    Jul 2013

    Posted 22 Nov 2013 Link to this post

    Hello
    I have control, which inherits from RadDateTimePicker. I use it across my whole application to ensure consistency.

    public class RadMaskedDatePicker : RadDateTimePicker
    {
        public RadMaskedDatePicker()
        {
            InitializeComponent();
        }
     
        private void InitializeComponent()
        {
            this.Format = DateTimePickerFormat.Short;
            InitializeFooter();
        }
     
        private void InitializeFooter()
        {
            RadDateTimePickerCalendar datePickerCalendarBehavior = this.DateTimePickerElement.GetCurrentBehavior() as RadDateTimePickerCalendar;
            if (datePickerCalendarBehavior == null)
            {
                return;
            }
            RadCalendar calendar = datePickerCalendarBehavior.Calendar;
            calendar.ShowFooter = true;
     
            CalendarStatusElement calendarStatusElement = calendar.CalendarElement.CalendarStatusElement;
            calendarStatusElement.RightToLeft = true;
            calendarStatusElement.LabelElement.Visibility = ElementVisibility.Hidden;
         
            calendarStatusElement.TodayButton.Text = Resources.strToday;
            calendarStatusElement.TodayButton.Click += (sender, args) =>
            {
                calendar.SelectedDates.Clear();
                this.Value = DateTime.Today;
                datePickerCalendarBehavior.PopupControl.HideControl();
            };
     
            calendarStatusElement.ClearButton.Text = Resources.strClear;
            calendarStatusElement.ClearButton.Click += (sender, args) =>
            {
                this.SetToNullValue();
                datePickerCalendarBehavior.PopupControl.HideControl();
            };
        }
     
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override DateTimePickerFormat Format
        {
            get { return base.Format; }
            set { base.Format = value; }
        }
    }

    Now I want to use it in RadGridView as an editor. I have tried to use some concepts from http://www.telerik.com/help/winforms/gridview-editors-using-custom-editors.html, but with no success.
     How can I achieve the desired functionality?
  2. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3945 posts

    Posted 27 Nov 2013 Link to this post

    Hello Patryk,

    Thank you for contacting Telerik Support.

    In order to replace the default RadDateTimeEditor in RadGridView, you should create derivative of the desired editor as it is described in our Using custom editors help article. Following this approach, you can replace any editor with a custom one:
    public Form1()
    {
        InitializeComponent();
     
        radGridView1.EditorRequired += radGridView1_EditorRequired;
    }
     
    private void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
    {
        if (e.EditorType == typeof(RadDateTimeEditor))
        {
            e.Editor = new CustomDateTimeEditor();
        }
    }

    public class CustomDateTimeEditor : RadDateTimeEditor
    {
        public CustomDateTimeEditor() : base()
        {
            InitializeComponent();
        }
     
        private void InitializeComponent()
        {
            ((RadDateTimePickerElement)this.EditorElement).Format = DateTimePickerFormat.Short;
            InitializeFooter();
        }
     
        private void InitializeFooter()
        {
            RadDateTimePickerElement editorElement = this.EditorElement as RadDateTimePickerElement;
            RadDateTimePickerCalendar datePickerCalendarBehavior = editorElement.GetCurrentBehavior() as RadDateTimePickerCalendar;
            if (datePickerCalendarBehavior == null)
            {
                return;
            }
            RadCalendar calendar = datePickerCalendarBehavior.Calendar;
            calendar.ShowFooter = true;
     
            CalendarStatusElement calendarStatusElement = calendar.CalendarElement.CalendarStatusElement;
            calendarStatusElement.RightToLeft = true;
            calendarStatusElement.LabelElement.Visibility = ElementVisibility.Hidden;
     
            calendarStatusElement.TodayButton.Text = "today";//Resources.strToday;
            calendarStatusElement.TodayButton.Click += (sender, args) =>
            {
                calendar.SelectedDates.Clear();
                this.Value = DateTime.Today;
                datePickerCalendarBehavior.PopupControl.HideControl();
            };
     
            calendarStatusElement.ClearButton.Text = "clear";//Resources.strClear;
            calendarStatusElement.ClearButton.Click += (sender, args) =>
            {
                ((RadDateTimePickerElement)this.EditorElement).SetToNullValue();
                datePickerCalendarBehavior.PopupControl.HideControl();
            };
        }
     
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public DateTimePickerFormat Format
        {
            get
            {
                return ((RadDateTimePickerElement)this.EditorElement).Format;
            }
            set
            {
                ((RadDateTimePickerElement)this.EditorElement).Format = value;
            }
        }
    }

    I hope this information helps. Should you have further questions, I would be glad to help.

    Regards,
    Desislava
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top