RepositoryItemLookUpEdit.MeasureColumn(GraphicsCache, LookUpColumnInfo) Method
Returns the width of the widest column value in pixels (taking the column’s caption into account). This overloaded method uses GraphicsCache for the sake of performance.
Namespace: DevExpress.XtraEditors.Repository
Assembly: DevExpress.XtraEditors.v24.1.dll
NuGet Package: DevExpress.Win.Navigation
Declaration
Parameters
Name | Type | Description |
---|---|---|
cache | GraphicsCache | The graphics cache. |
column | LookUpColumnInfo | The column being processed. |
Returns
Type | Description |
---|---|
Int32 | The 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.
The 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 code sample below illustrates how to use the 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 code sample is provided for demonstration purposes only.
In the code, the 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 sufficient widths to display all values in their entirety.
Note
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. Also please add the width of the shadow and borders (4 pixels).
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Controls;
//...
RepositoryItemLookUpEdit riLookupEdit = lookUpEdit1.Properties;
GraphicsCache gc = new GraphicsCache();
try {
int totalWidth = 0;
for (int i = 0; i < riLookupEdit.Columns.Count; i++) {
LookUpColumnInfo column = riLookupEdit.Columns[i];
int colWidth = riLookupEdit.MeasureColumn(gc, column);
column.Width = colWidth;
totalWidth += colWidth;
}
riLookupEdit.PopupWidth = totalWidth + SystemInformation.VerticalScrollBarWidth + 4;
}
finally {
gc.Dispose();
}