Back to Devexpress

RepositoryItemLookUpEdit.PopulateColumns() Method

windowsforms-devexpress-dot-xtraeditors-dot-repository-dot-repositoryitemlookupedit-73def0fb.md

latest8.9 KB
Original Source

RepositoryItemLookUpEdit.PopulateColumns() Method

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

Namespace : DevExpress.XtraEditors.Repository

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
public virtual void PopulateColumns()
vb
Public Overridable Sub 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.

csharp
using DevExpress.XtraEditors.Repository;

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

    // Initializes the lookup's data source and internal infrastructure.
    repositoryItemLookUpEdit.ForceInitialize();

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

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

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim repositoryItemLookUpEdit As New RepositoryItemLookUpEdit()
    repositoryItemLookUpEdit.DataSource = New List(Of DataObject) From {
        New DataObject With {.ID = 1, .Text = "One"},
        New DataObject With {.ID = 2, .Text = "Two"},
        New DataObject With {.ID = 3, .Text = "Three"}
    }
    repositoryItemLookUpEdit.ValueMember = nameof(DataObject.ID)
    repositoryItemLookUpEdit.DisplayMember = nameof(DataObject.Text)

    ' Initializes the lookup's data source and internal infrastructure.
    repositoryItemLookUpEdit.ForceInitialize()

    ' Now, you can safely populate lookup columns.
    repositoryItemLookUpEdit.PopulateColumns()

    ' Access columns and obtain column values.
    Dim column = repositoryItemLookUpEdit.Columns(nameof(DataObject.Text))
    Dim columnValue As String = TryCast(repositoryItemLookUpEdit.GetDataSourceValue(column, 1), String) ' columnValue = "Two"
    Dim keyValue As Object = repositoryItemLookUpEdit.GetKeyValueByDisplayValue(column) ' keyValue = 2
End Sub

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.

winforms-workspacemanager-saving-load-app-layout/CS/T190543/Form1.cs#L36

csharp
this.repositoryItemLookUpEdit1.PopulateColumns();
this.repositoryItemLookUpEdit1.Columns[1].Visible = false;

how-to-use-the-processnewvalue-event-of-a-lookup-editor-e285/CS/Form1.cs#L33

csharp
LookUpEdit1.Properties.DisplayMember = "Name";
LookUpEdit1.Properties.PopulateColumns();
LookUpEdit1.Properties.AutoSearchColumnIndex = 1;

winforms-workspacemanager-saving-load-app-layout/VB/T190543/Form1.vb#L37

vb
repositoryItemLookUpEdit1.ValueMember = "Name"
repositoryItemLookUpEdit1.PopulateColumns()
repositoryItemLookUpEdit1.Columns(1).Visible = False

how-to-use-the-processnewvalue-event-of-a-lookup-editor-e285/VB/Form1.vb#L35

vb
LookUpEdit1.Properties.DisplayMember = "Name"
LookUpEdit1.Properties.PopulateColumns()
LookUpEdit1.Properties.AutoSearchColumnIndex = 1

See Also

Columns

LookUpColumnInfo

BestFit()

RepositoryItemLookUpEdit Class

RepositoryItemLookUpEdit Members

DevExpress.XtraEditors.Repository Namespace