New to Telerik UI for WinForms? Download free 30-day trial

Change auto generated editor

By default RadDataEntry generates several different editors according to the data type of the property that it should edit. The following table demonstrates the default editors that RadDataEntry can create.

enum RadDropDownList
DateTime RadDateTimePicker
Boolean RadCheckBox
Color RadColorBox
Image PictureBox
string RadTextBox
Byte, SByte, UInt16, UInt32, UInt64, Int16, Int32, Int64, Single, Double, Decimal RadSpinEditor

For any type that is not represented in this table RadDataEntry generates RadTextBox .

In the following example it will be demonstrated how to change default editor with the custom one.

1. For the purpose of this tutorial, we will create a new class Employee with a couple of exposed properties. By binding RadDataEntry to object from this type we will generate several items.

Data Object

private class Employee
{
    public string FirstName
    {
        get;
        set;
    }
    public string LastName
    {
        get;
        set;
    }
    public string Occupation
    {
        get;
        set;
    }
    public DateTime StartingDate
    {
        get;
        set;
    }
    public bool IsMarried
    {
        get;
        set;
    }
    public int Salary
    {
        get;
        set;
    }
    public Gender Gender
    {
        get;
        set;
    }
}
private enum Gender
{
    Female,
    Male
}

Private Class Employee
    Public Property FirstName() As String
        Get
            Return m_FirstName
        End Get
        Set(value As String)
            m_FirstName = Value
        End Set
    End Property
    Private m_FirstName As String
    Public Property LastName() As String
        Get
            Return m_LastName
        End Get
        Set(value As String)
            m_LastName = Value
        End Set
    End Property
    Private m_LastName As String
    Public Property Occupation() As String
        Get
            Return m_Occupation
        End Get
        Set(value As String)
            m_Occupation = Value
        End Set
    End Property
    Private m_Occupation As String
    Public Property StartingDate() As DateTime
        Get
            Return m_StartingDate
        End Get
        Set(value As DateTime)
            m_StartingDate = Value
        End Set
    End Property
    Private m_StartingDate As DateTime
    Public Property IsMarried() As Boolean
        Get
            Return m_IsMarried
        End Get
        Set(value As Boolean)
            m_IsMarried = Value
        End Set
    End Property
    Private m_IsMarried As Boolean
    Public Property Salary() As Integer
        Get
            Return m_Salary
        End Get
        Set(value As Integer)
            m_Salary = Value
        End Set
    End Property
    Private m_Salary As Integer
    Public Property Gender() As Gender
        Get
            Return m_Gender
        End Get
        Set(value As Gender)
            m_Gender = Value
        End Set
    End Property
    Private m_Gender As Gender
End Class
Private Enum Gender
    Female
    Male
End Enum

Data Binding

this.radDataEntry1.DataSource = new Employee() 
{ 
    FirstName = "Sarah",
    LastName = "Blake",
    Occupation = "Supplied Manager", 
    StartingDate = new DateTime(2005, 04, 12),
    IsMarried = true, 
    Salary = 3500, Gender = Gender.Female 
};

Me.radDataEntry1.DataSource = New Employee() With { _
  .FirstName = "Sarah", _
  .LastName = "Blake", _
  .Occupation = "Supplied Manager", _
  .StartingDate = New DateTime(2005, 4, 12), _
  .IsMarried = True, _
  .Salary = 3500, _
  .Gender = Gender.Female _
 }

Figure 1: RadDataEntry Initializing.

WinForms RadDataEntry Initializing

2. To change the default RadTextBox editor of the “Salary” property with RadMaskedEditBox we will subscribe to EditorInitializing event of RadDataEntry.

void radDataEntry1_EditorInitializing(object sender, Telerik.WinControls.UI.EditorInitializingEventArgs e)
{
    if (e.Property.Name == "Salary")
    {
        RadMaskedEditBox radMaskedEditBox = new RadMaskedEditBox();
        radMaskedEditBox.MaskType = MaskType.Numeric;
        radMaskedEditBox.MaskedEditBoxElement.StretchVertically = true;
        e.Editor = radMaskedEditBox;
    }
}

Private Sub radDataEntry1_EditorInitializing(sender As Object, e As Telerik.WinControls.UI.EditorInitializingEventArgs)
    If e.[Property].Name = "Salary" Then
        Dim radMaskedEditBox As New RadMaskedEditBox()
        radMaskedEditBox.MaskType = MaskType.Numeric
        radMaskedEditBox.MaskedEditBoxElement.StretchVertically = True
        e.Editor = radMaskedEditBox
    End If
End Sub

3. To achieve working binding for this new editor we should subscribe to the BindingCreated event where we will subscribe to the Parse event of the Binding object. You can read more about Format and Parse events of Binding object and why we should use them here.

Subscribe to Parse Event

void radDataEntry1_BindingCreated(object sender, BindingCreatedEventArgs e)
{
    if (e.DataMember == "Salary")
    {
        e.Binding.Parse += new ConvertEventHandler(Binding_Parse);
    }
}
void Binding_Parse(object sender, ConvertEventArgs e)
{
    int salary = int.Parse(e.Value.ToString(), NumberStyles.Currency);
    e.Value = salary;
}

Private Sub radDataEntry1_BindingCreated(sender As Object, e As BindingCreatedEventArgs)
    If e.DataMember = "Salary" Then
        AddHandler e.Binding.Parse, AddressOf Binding_Parse
    End If
End Sub
Private Sub Binding_Parse(sender As Object, e As ConvertEventArgs)
    Dim salary As Integer = Integer.Parse(e.Value.ToString(), NumberStyles.Currency)
    e.Value = salary
End Sub

Figure 2: RadDataEntry MaskedEditBox.

WinForms RadDataEntry MaskedEditBox

RadSpinEditor Default Values

The spin editor is created with the default settings for Minimum/Maximum, DecimalPlaces, and Step. If a case, you are using fractional numbers, the decimal part will be lost so we need to change the above properties. You can do that in the EditorInitializing.

void radDataEntry_EditorInitializing(object sender, EditorInitializingEventArgs e)
{
  var spinEditor = e.Editor as RadSpinEditor;

  if (spinEditor == null)
    return;

  spinEditor.Step = 0.01m;
  spinEditor.DecimalPlaces = 2;
  spinEditor.Maximum = Decimal.MaxValue;
}

Private Sub radDataEntry_EditorInitializing(ByVal sender As Object, ByVal e As EditorInitializingEventArgs)
    Dim spinEditor = TryCast(e.Editor, RadSpinEditor)
    If spinEditor Is Nothing Then Return
    spinEditor.[Step] = 0.01D
    spinEditor.DecimalPlaces = 2
    spinEditor.Maximum = Decimal.MaxValue
End Sub

See Also

In this article