windowsforms-116016-controls-and-libraries-editors-and-simple-controls-lookup-editors-advanced-binding-to-business-objects.md
In standard lookup mode, a lookup’s value is of a simple data type (for example, integer or string). Advanced or extended binding means that the lookup’s value is a business object that implements its own identity and uniqueness (the business object must have a key field). When a user selects an item in the dropdown, the lookup assigns the corresponding business object to the lookUpEdit.EditValue property.
// Sets the lookup's value to a 'Department' business object.
lookupProducts.EditValue = new Department(1);
public class Department {
int id;
public Department(int id) {
this.id = id;
}
public int DepartmentID { get { return id; } }
public string Name { get; set; }
static public List<Department> LoadData() {
return new List<Department>() {
new Department(0){ Name = "Development" },
new Department(1){ Name = "Security & QA" },
new Department(2){ Name = "Marketing" },
new Department(3){ Name = "UI/UX Design" }
};
}
}
' Sets the lookup's value to a 'Department' business object.
lookupProducts.EditValue = New Department(1)
Public Class Department
Private id As Integer
Public Sub New(ByVal id As Integer)
Me.id = id
End Sub
Public ReadOnly Property DepartmentID() As Integer
Get
Return id
End Get
End Property
Public Property Name() As String
Public Shared Function LoadData() As List(Of Department)
Return New List(Of Department)() From {
New Department(0) With {.Name = "Development"},
New Department(1) With {.Name = "Security & QA"},
New Department(2) With {.Name = "Marketing"},
New Department(3) With {.Name = "UI/UX Design"}
}
End Function
End Class
Set the following properties to bind a lookup to a business object:
DataSource – Specifies the source of records.
KeyMember – Specifies a key field (or multiple key fields) of business objects.
DisplayMember – The data source field whose values are visible to users. A value from this field is displayed in the lookup’s text box when a user selects a record.
// Binds the lookup to a business object (Department).
lookupProducts.Properties.DataSource = Department.LoadData();
lookupProducts.Properties.DisplayMember = "Name";
lookupProducts.Properties.KeyMember = "DepartmentID";
// Sets the lookup's value to a new 'Department' business object.
lookupProducts.EditValue = new Department(1);
' Binds the lookup to a business object (Department).
lookupProducts.Properties.DataSource = Department.LoadData()
lookupProducts.Properties.DisplayMember = "Name"
lookupProducts.Properties.KeyMember = "DepartmentID"
' Sets the lookup's value to a new 'Department' business object.
lookupProducts.EditValue = New Department(1)
This example demonstrates how to bind a standalone lookup to a business object.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public Form1() {
InitializeComponent();
// Binds the lookup to a business object (Department).
lookupProducts.Properties.DataSource = Department.LoadData();
lookupProducts.Properties.DisplayMember = "Name";
lookupProducts.Properties.KeyMember = "DepartmentID";
// Subscribes to the lookup's EditValueChanged event.
lookupProducts.EditValueChanged += new EventHandler(lookupProducts_EditValueChanged);
// Sets the lookup's value to a 'Department' business object.
lookupProducts.EditValue = new Department(1);
}
private void lookupProducts_EditValueChanged(object sender, EventArgs e) {
// The lookup's new value (a 'Department' object).
object value = lookupProducts.EditValue;
}
public class Department {
int id;
public Department(int id) {
this.id = id;
}
public int DepartmentID { get { return id; } }
[Display(ShortName = "Department")]
public string Name { get; set; }
public string Location { get; set; }
static public List<Department> LoadData() {
return new List<Department>() {
new Department(0){ Name = "Development", Location = "LA, California" },
new Department(1){ Name = "Security & QA", Location = "Seattle, Washington" },
new Department(2){ Name = "Marketing", Location = "Miami, Florida" },
new Department(3){ Name = "UI/UX Design", Location = "Los Angeles, California" }
};
}
}
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel.DataAnnotations
Public Sub New()
InitializeComponent()
' Binds the lookup to a business object (Department).
lookupProducts.Properties.DataSource = Department.LoadData()
lookupProducts.Properties.DisplayMember = "Name"
lookupProducts.Properties.KeyMember = "DepartmentID"
' Subscribes to the lookup's EditValueChanged event.
AddHandler lookupProducts.EditValueChanged, AddressOf lookupProducts_EditValueChanged
' Sets the lookup's value to a 'Department' business object.
lookupProducts.EditValue = New Department(1)
End Sub
Private Sub lookupProducts_EditValueChanged(ByVal sender As Object, ByVal e As EventArgs)
' The lookup's new value (a 'Department' object).
Dim value As Object = lookupProducts.EditValue
End Sub
Public Class Department
Private id As Integer
Public Sub New(ByVal id As Integer)
Me.id = id
End Sub
Public ReadOnly Property DepartmentID() As Integer
Get
Return id
End Get
End Property
<Display(ShortName := "Department")>
Public Property Name() As String
Public Property Location() As String
Public Shared Function LoadData() As List(Of Department)
Return New List(Of Department)() From {
New Department(0) With {.Name = "Development", .Location = "LA, California"},
New Department(1) With {.Name = "Security & QA", .Location = "Seattle, Washington"},
New Department(2) With {.Name = "Marketing", .Location = "Miami, Florida"},
New Department(3) With {.Name = "UI/UX Design", .Location = "Los Angeles, California"}
}
End Function
End Class
This example demonstrates how to bind an in-place lookup to a business object.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraEditors.Repository;
public Form1() {
InitializeComponent();
// Binds the grid control to data and initializes the grid columns.
gridControl1.DataSource = Task.LoadData();
gridControl1.ForceInitialize();
gridControl1.DefaultView.PopulateColumns();
// Creates and initializes a lookup repository item.
RepositoryItemLookUpEdit lookup = new RepositoryItemLookUpEdit();
lookup.DataSource = Department.LoadData();
lookup.DisplayMember = "Name";
lookup.KeyMember = "DepartmentID";
// Assigns the lookup repository item to the grid's 'Department' column.
((ColumnView)gridControl1.DefaultView).Columns["Department"].ColumnEdit = lookup;
}
public class Task {
int id;
public Task(int id) {
this.id = id;
}
[Display(Order = -1)]
public int ID {
get { return id; }
}
public string Title { get; set; }
public DateTime CreateDate { get; set; }
public Department Department { get; set; }
static public List<Task> LoadData() {
return new List<Task>() {
new Task(0){ Title = "Export to PDF API", CreateDate = DateTime.Now, Department = new Department(0) },
new Task(0){ Title = "Software Accessibility & Security", CreateDate = DateTime.Now, Department = new Department(1) },
new Task(0){ Title = "Update Pricing Options", CreateDate = DateTime.Now, Department = new Department(2) },
};
}
}
public class Department {
int id;
public Department(int id) {
this.id = id;
}
public int DepartmentID { get { return id; } }
[Display(ShortName = "Department")]
public string Name { get; set; }
public string Location { get; set; }
static public List<Department> LoadData() {
return new List<Department>() {
new Department(0){ Name = "Development", Location = "LA, California" },
new Department(1){ Name = "Security & QA", Location = "Seattle, Washington" },
new Department(2){ Name = "Marketing", Location = "Miami, Florida" },
new Department(3){ Name = "UI/UX Design", Location = "Los Angeles, California" }
};
}
}
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel.DataAnnotations
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraEditors.Repository
Public Sub New()
InitializeComponent()
' Binds the grid control to data and initializes the grid columns.
gridControl1.DataSource = Task.LoadData()
gridControl1.ForceInitialize()
gridControl1.DefaultView.PopulateColumns()
' Creates and initializes a lookup repository item.
Dim lookup As New RepositoryItemLookUpEdit()
lookup.DataSource = Department.LoadData()
lookup.DisplayMember = "Name"
lookup.KeyMember = "DepartmentID"
' Assigns the lookup repository item to the grid's 'Department' column.
CType(gridControl1.DefaultView, ColumnView).Columns("Department").ColumnEdit = lookup
End Sub
Public Class Task
Private fId As Integer
Public Sub New(ByVal id As Integer)
Me.fId = id
End Sub
<Display(Order := -1)>
Public ReadOnly Property ID() As Integer
Get
Return fId
End Get
End Property
Public Property Title() As String
Public Property CreateDate() As Date
Public Property Department() As Department
Public Shared Function LoadData() As List(Of Task)
Return New List(Of Task)() From {
New Task(0) With {.Title = "Export to PDF API", .CreateDate = Date.Now, .Department = New Department(0)},
New Task(0) With {.Title = "Software Accessibility & Security", .CreateDate = Date.Now, .Department = New Department(1)},
New Task(0) With {.Title = "Update Pricing Options", .CreateDate = Date.Now, .Department = New Department(2)}
}
End Function
End Class
Public Class Department
Private id As Integer
Public Sub New(ByVal id As Integer)
Me.id = id
End Sub
Public ReadOnly Property DepartmentID() As Integer
Get
Return id
End Get
End Property
<Display(ShortName := "Department")>
Public Property Name() As String
Public Property Location() As String
Public Shared Function LoadData() As List(Of Department)
Return New List(Of Department)() From {
New Department(0) With {.Name = "Development", .Location = "LA, California"},
New Department(1) With {.Name = "Security & QA", .Location = "Seattle, Washington"},
New Department(2) With {.Name = "Marketing", .Location = "Miami, Florida"},
New Department(3) With {.Name = "UI/UX Design", .Location = "Los Angeles, California"}
}
End Function
End Class
This example demonstrates how to display SVG icons from the SvgImageCollection within the LookUp editor’s dropdown.
using System;
using System.Collections.Generic;
using DevExpress.Utils;
using DevExpress.Utils.Svg;
public partial class Form1 : DevExpress.XtraEditors.XtraForm {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
lookUpEdit1.Properties.AllowNullInput = DefaultBoolean.True;
lookUpEdit1.Properties.DataSource = InitData();
lookUpEdit1.Properties.ValueMember = "ID";
lookUpEdit1.Properties.DisplayMember = "DocumentFormat";
}
List<DataObject> InitData() {
return new List<DataObject>() {
new DataObject(0, "MS Word Binary File Format") { SvgImage = svgImageCollection1[0] },
new DataObject(1, "Portable Document Format") { SvgImage = svgImageCollection1[1] },
new DataObject(2, "Microsoft Excel Spreadsheet") { SvgImage = svgImageCollection1[2] },
new DataObject(3, "Extensible Markup Language") { SvgImage = svgImageCollection1[3] }
};
}
}
public class DataObject {
int fId;
public DataObject(int fId, string format) {
this.fId = fId;
DocumentFormat = format;
}
public int ID {
get { return fId; }
}
public SvgImage SvgImage { get; set; }
public string DocumentFormat { get; set; }
}
Imports System
Imports System.Collections.Generic
Imports DevExpress.Utils
Imports DevExpress.Utils.Svg
Partial Public Class Form1
Inherits DevExpress.XtraEditors.XtraForm
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
lookUpEdit1.Properties.AllowNullInput = DefaultBoolean.True
lookUpEdit1.Properties.DataSource = InitData()
lookUpEdit1.Properties.ValueMember = "ID"
lookUpEdit1.Properties.DisplayMember = "DocumentFormat"
End Sub
Private Function InitData() As List(Of DataObject)
Return New List(Of DataObject)() From {
New DataObject(0, "MS Word Binary File Format") With {.SvgImage = svgImageCollection1(0)},
New DataObject(1, "Portable Document Format") With {.SvgImage = svgImageCollection1(1)},
New DataObject(2, "Microsoft Excel Spreadsheet") With {.SvgImage = svgImageCollection1(2)},
New DataObject(3, "Extensible Markup Language") With {.SvgImage = svgImageCollection1(3)}
}
End Function
End Class
Public Class DataObject
Private fId As Integer
Public Sub New(ByVal fId As Integer, ByVal format As String)
Me.fId = fId
DocumentFormat = format
End Sub
Public ReadOnly Property ID() As Integer
Get
Return fId
End Get
End Property
Public Property SvgImage() As SvgImage
Public Property DocumentFormat() As String
End Class
See Also