Skip to main content

Bind Form Layout to a Single Object

  • 3 minutes to read

This tutorial describes how to bind the FormLayout control to a single object. You will learn how to use the FormLayout to edit a record from a data table, retrieved from a database via LINQ to SQL.

To bind FormLayout to an object, follow the steps below.

  1. Drop a FormLayout onto the form. Click the smart tag of the FormLayout, and select Edit Layout in the invoked actions list. In the invoked ASPxFormLayout Items Editor, click Retrieve Items.

    FormLayout_BindingToSingleObject_Retrieve

  2. In the Choose Data Type… dialog, select the type of objects whose fields will be edited/displayed within the FormLayout. In this example, the Employee class is selected.

    FormLayout_BindingToSingleObject_EditedType

  3. The FormLayout control will automatically generate layout items for data source fields. The LayoutItem.FieldName property value of each automatically generated layout item is set to a corresponding data source field name.

    The table below illustrates which DevExpress Data Editors are automatically nested into FormLayout for different field value types. Nullable types are also supported.

    Value Type Automatically Nested Data Editor
    byte[] BinaryImage
    String, Char TextBox
    Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, Double, Decimal SpinEdit
    Enum ComboBox
    Boolean CheckBox
    DateTime DateEdit

    The image below illustrates automatically generated fields for the Employee class used in this tutorial.

    FormLayout_BindingToSingleObject_RawItems

  4. You can add/delete/edit any layout item according to your needs (e.g., you can group layout items, add unbound layout items, customize automatically nested controls, etc. Follow the Item Manipulation topic to learn how to customize the layout of items and groups.

    The image below illustrates how the position of layout items can be changed.

    FormLayout_BindingToSingleObject_NewLayout

  5. Define a data source within the code-behind. When updating data source values, retrieve new values via the ASPxFormLayout.GetNestedControlValueByFieldName method. The code sample below demonstrates how to bind FormLayout to a data source via LINQ to SQL, and update values in the database when an end-user submits the form to the server.

    using System;
    using System.Linq;
    
    namespace FormLayoutDemo
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            private EmployeeDataContext dc = new EmployeeDataContext();
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    var employee = dc.Employees.First(emp => emp.EmployeeID == 4);
                    ASPxFormLayout1.DataSource = employee;
                    ASPxFormLayout1.DataBind();
                }
            }
    
            protected void submitButton_Click(object sender, EventArgs e)
            {
                Employee employee = dc.Employees.First(emp => emp.EmployeeID == 4);
    
                employee.FirstName = (string)ASPxFormLayout1.GetNestedControlValueByFieldName("FirstName");
                employee.LastName = (string)ASPxFormLayout1.GetNestedControlValueByFieldName("LastName");
                employee.BirthDate = (DateTime?)ASPxFormLayout1.GetNestedControlValueByFieldName("BirthDate");
                employee.HireDate = (DateTime?)ASPxFormLayout1.GetNestedControlValueByFieldName("HireDate");
                employee.Country = (string)ASPxFormLayout1.GetNestedControlValueByFieldName("Country");
                employee.City = (string)ASPxFormLayout1.GetNestedControlValueByFieldName("City");
                dc.SubmitChanges();
            }
        }
    }
    

    Note

    You can also bind the FormLayout control to a list of objects (IEnumerable<T>, IQueryable<T>, etc.). In this case, the first element in the list is automatically retrieved.

The image below shows the result.

FormLayout_BindingToSingleObject_Result

See Also