Back to Devexpress

How to: Bind a Standalone Editor in Code

windowsforms-3111-controls-and-libraries-editors-and-simple-controls-examples-data-binding-how-to-bind-a-standalone-editor-in-code.md

latest2.0 KB
Original Source

How to: Bind a Standalone Editor in Code

  • Oct 29, 2020

To bind a standalone editor in code, you need to add a Binding object to the control’s DataBindings collection. The FormattingEnabled property must be set to true.

The following example shows how to bind a DateEdit control to a nullable property of an Entity business object.

csharp
// An Entity object.
Entity _entity = new Entity();
// Bind the editor to the Entity.NullableDateTime property. 
// Set the Binding.FormattingEnabled property to true.
dateEdit1.DataBindings.Add("EditValue", _entity, "NullableDateTime", true);

public class Entity {
    public Entity() { }

    private DateTime? _nullableDateTime = new DateTime(2001, 1, 1);

    public DateTime? NullableDateTime {
        get { return _nullableDateTime; }
        set { _nullableDateTime = value; }
    }
}
vb
' An Entity object.
Dim _entity As Entity = New Entity
' Bind the editor to the Entity.NullableDateTime property. 
' Set the Binding.FormattingEnabled property to true.
Me.DateEdit1.DataBindings.Add(New System.Windows.Forms.Binding( _ 
    "EditValue", _entity, "NullableDateTime", True))

Public Class Entity
    Public Sub New()
    End Sub

    Private _nullableDateTime As Nullable(Of DateTime) = New DateTime(2001, 1, 1)

    Public Property NullableDateTime() As Nullable(Of DateTime)
        Get
            Return _nullableDateTime
        End Get
        Set(ByVal value As Nullable(Of DateTime))
            _nullableDateTime = value
        End Set
    End Property
End Class