This is a migrated thread and some comments may be shown as answers.

Change datetime editor in RadGridView to inherited control

1 Answer 155 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Patryk
Top achievements
Rank 1
Patryk asked on 22 Nov 2013, 10:01 AM
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?

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 27 Nov 2013, 07:46 AM
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 >>
Tags
GridView
Asked by
Patryk
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or