Back to Devexpress

How to: Create Column with Custom Data in LookUpEdit

windowsforms-9497-controls-and-libraries-editors-and-simple-controls-examples-how-to-create-column-with-custom-data-in-lookupedit.md

latest5.6 KB
Original Source

How to: Create Column with Custom Data in LookUpEdit

  • Feb 04, 2023
  • 4 minutes to read

The code below shows how to make a lookup editor display data from a custom array. The editor contains two columns. One column is bound to a field from the data source. The other column is unbound and is populated via the RepositoryItemLookUpEdit.GetNotInListValue event.

The data source of a lookup editor represents an array of Record class objects. The Record class declares the public Country property whose values are displayed in the column of the same name. The lookup editor also contains an ID column. Its field name (“ID”) does not coincide with any fields from the data source. So this column is unbound and is populated via the RepositoryItemLookUpEdit.GetNotInListValue event. In the event handler, we return the record id as a value for the column.

The following image shows the lookup editor after running this code:

csharp
using DevExpress.XtraEditors.Controls;

//The class representing a row in a lookup editor
public class Record {
    private string country;
    public Record(string country) {
        this.country = country;
    }
    public string Country {
        get { return country; }
        set { country = value; }
    }
}

// ...
// An array of records displayed in the dropdown in the lookup editor
Record[] records = null;

//Fill the records array
private void InitData() {
    string[] data = (new string[] {"United States", "Afghanistan", "Albania", "Algeria",
  "Andorra", "Angola"});
    records = new Record[data.Length];
    for (int i = 0; i < data.Length; i++)
        records[i] = new Record(data[i]);
}

//Set up the lookup editor
private void InitLookUp() {
    //Specify an array of countries as a data source
    lookUpEdit2.Properties.DataSource = records;
    //The field whose values are displayed in the edit box
    lookUpEdit2.Properties.DisplayMember = "Country";
    //The field whose values match the edit value
    lookUpEdit2.Properties.ValueMember = "Country";

    //Unbound column
    lookUpEdit2.Properties.Columns.Add(new LookUpColumnInfo("ID", "ID", 20));
    //Column bound to the existing 'Country' field from the data source
    lookUpEdit2.Properties.Columns.Add(new LookUpColumnInfo("Country", "Country", 100));

    //Select the first record from the dropdown
    lookUpEdit2.EditValue = records[0].Country;
}

private void Form1_Load(object sender, System.EventArgs e) {
    InitData();
    InitLookUp();
}

private void lookUpEdit2_GetNotInListValue(object sender, GetNotInListValueEventArgs e) 
    //provide a value for the ID field
    if (e.FieldName == "ID")
        e.Value = e.RecordIndex.ToString() + ".";
}
vb
'The class representing a row in a lookup editor
Public Class Record
    Private _country As String
    Public Sub New(ByVal _country As String)
        Me._country = _country
    End Sub
    Public Property Country() As String
        Get
            Return _country
        End Get
        Set(ByVal Value As String)
            _country = Value
        End Set
    End Property
End Class

' ...
'An array of records displayed in the dropdown in the lookup editor
Dim records() As Record = Nothing

'Fill the records array
Private Sub InitData()
    Dim _data() As String = New String() {"United States", "Afghanistan", "Albania", _
      "Algeria", "Andorra", "Angola"}
    records = New Record(_data.Length - 1) {}
    Dim I
    For I = 0 To _data.Length - 1
        records(I) = New Record(_data(I))
    Next
End Sub

'Set up the lookup editor
Private Sub InitLookUp()
    'Specify an array of countries as a data source
    LookUpEdit2.Properties.DataSource = records
    'The field whose values are displayed in the edit box
    LookUpEdit2.Properties.DisplayMember = "Country"
    'The field whose values match the edit value
    LookUpEdit2.Properties.ValueMember = "Country"

    'Unbound column
    LookUpEdit2.Properties.Columns.Add(New LookUpColumnInfo("ID", "ID", 20))
    'Column bound to the existing 'Country' field from the data source
    LookUpEdit2.Properties.Columns.Add(New LookUpColumnInfo("Country", "Country", 100))

    'Select the first record from the dropdown
    LookUpEdit2.EditValue = records(0).Country
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
  Handles MyBase.Load
    InitData()
    InitLookUp()
End Sub

Private Sub LookUpEdit2_GetNotInListValue(ByVal sender As Object, _
  ByVal e As DevExpress.XtraEditors.Controls.GetNotInListValueEventArgs) _
  Handles LookUpEdit2.GetNotInListValue
    'provide a value for the ID field
    If (e.FieldName = "ID") Then e.Value = e.RecordIndex.ToString() + "."
End Sub

See Also

Lookup Main Settings

Standard Binding (to Simple Data Types)

Advanced Binding (to Business Objects)

Bind Lookup to Dictionary