Skip to main content
A newer version of this page is available. .

RepositoryItemLookUpEdit.PopulateColumns() Method

Creates columns for all fields available in the lookup data source (RepositoryItemLookUpEditBase.DataSource).

Namespace: DevExpress.XtraEditors.Repository

Assembly: DevExpress.XtraEditors.v22.1.dll

NuGet Package: DevExpress.Win.Navigation

Declaration

public virtual void PopulateColumns()

Remarks

If no columns are explicitly created in the RepositoryItemLookUpEdit.Columns collection, the lookup editor automatically calls the PopulateColumns method to create columns at runtime when the editor’s dropdown is invoked for the first time.

Note

When the form is loading, call the ForceInitialize() method, then call the PopulateColumns method before you start to create columns or obtain column values.

This example demonstrates how to initialize a LookUpEdit or its repository item during form load.

using DevExpress.XtraEditors.Repository;

private void Form1_Load(object sender, EventArgs e) {
    RepositoryItemLookUpEdit lookUpEdit = new RepositoryItemLookUpEdit();
    lookUpEdit.DataSource = new List<DataObject> {
        new DataObject { ID = 1, Text = "One" },
        new DataObject { ID = 2, Text = "Two" },
        new DataObject { ID = 3, Text = "Three" }
    };
    lookUpEdit.ValueMember = nameof(DataObject.ID);
    lookUpEdit.DisplayMember = nameof(DataObject.Text);

    // Initializes the lookup's data source and internal infrustructure.
    lookUpEdit.ForceInitialize();

    // Now, you can safely populate lookup columns.
    lookUpEdit.PopulateColumns();

    // Access columns and obtain column values.
    var column = lookUpEdit.Columns[nameof(DataObject.Text)];
    string columnValue = lookUpEdit.GetDataSourceValue(column, 1) as string; // columnValue = "Two"
    object keyValue = lookUpEdit.GetKeyValueByDisplayValue(column); // keyValue = 2
}

The PopulateColumns method removes the existing columns from the RepositoryItemLookUpEdit.Columns collection and adds new columns to this collection for all fields in the RepositoryItemLookUpEditBase.DataSource. Use traditional collection methods to add, remove, and access individual lookup columns.

At design time, you can retrieve fields from the bound lookup data source and create corresponding columns via the control’s smart tag (for standalone editors) or by clicking the Populate Columns button in the Column Collection Editor. To activate this editor, click the ellipsis button for the RepositoryItemLookUpEdit.Columns property in the Properties window.

Each column in a lookup editor is an instance of the LookUpColumnInfo class. The PopulateColumns method sets the LookUpColumnInfo.FieldName and LookUpColumnInfo.Caption properties to the corresponding field names in the lookup data source.

If a field specifies a date/time value, the corresponding column’s LookUpColumnInfo.FormatType is set to FormatType.DateTime and FormatInfo.FormatString is set to the Short Date Format.

For numeric fields, the column’s LookUpColumnInfo.FormatType is set to FormatType.Numeric and the LookUpColumnInfo.Alignment is set to HorzAlignment.Far. For non-numeric fields, the alignment is set to HorzAlignment.Near.

After all columns have been created, the method calls RepositoryItemLookUpEdit.BestFit in order to calculate column widths according to their contents.

The following code snippets (auto-collected from DevExpress Examples) contain references to the PopulateColumns() method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also