Skip to main content

RepositoryItemLookUpEdit.MeasureColumn(Graphics, LookUpColumnInfo) Method

Returns the width of the widest column value in pixels (taking the column’s caption into account).

Namespace: DevExpress.XtraEditors.Repository

Assembly: DevExpress.XtraEditors.v24.2.dll

Declaration

public int MeasureColumn(
    Graphics g,
    LookUpColumnInfo column
)

Parameters

Name Type Description
g Graphics

The graphics surface.

column LookUpColumnInfo

The column being processed.

Returns

Type Description
Int32

The a column width.

Remarks

The method loops through column values and returns the width, in pixels, of the widest string. It also measures the width of the column’s caption and returns the greater of the two values. The column being processed is determined by the column parameter.

MeasureColumn is called from the RepositoryItemLookUpEdit.BestFit method, which sets the widths of all the columns according to their contents.

A value returned by MeasureColumn can be assigned to the column’s LookUpColumnInfo.Width property. The total width of the dropdown window is specified by RepositoryItemLookUpEdit.PopupWidth.

The Graphics object for the method can be obtained via the CreateGraphics() method.

Example

The following example shows how to use the RepositoryItemLookUpEdit.MeasureColumn method to support best-fit functionality for columns in a lookup editor. However, note that this functionality can also be implemented by using a single RepositoryItemLookUpEditBase.BestFitMode property. The following example is for demonstration purposes only.

In the code the RepositoryItemLookUpEdit.MeasureColumn method is invoked for all columns in a lookup editor. The calculated width is then assigned to the LookUpColumnInfo.Width property. After all columns are processed, the sum of calculated widths is assigned to RepositoryItemLookUpEdit.PopupWidth. This ensures that all columns have widths sufficient to display all values in their entirety.

We need to take into account the width of the vertical scroll bar which would appear if the number of rows in the data source is greater than the RepositoryItemLookUpEdit.DropDownRows property value. We must also add the width of the shadow and borders (4 pixels).

using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Controls;
//...
RepositoryItemLookUpEdit riLookupEdit = lookUpEdit1.Properties;
Graphics g = lookupEdit1.CreateGraphics();
try {
    int totalWidth = 0;
    for (int i = 0; i < riLookupEdit.Columns.Count; i++) {
        LookUpColumnInfo column = riLookupEdit.Columns[i];
        int colWidth = riLookupEdit.MeasureColumn(g, column);
        column.Width = colWidth;
        totalWidth += colWidth;
    }
    riLookupEdit.PopupWidth = totalWidth + SystemInformation.VerticalScrollBarWidth + 4;
}
finally {
    g.Dispose();
}
See Also