Alignment.ShrinkToFit Property
Specifies whether text size in a cell changes automatically to fit the cell width.
Namespace: DevExpress.Spreadsheet
Assembly: DevExpress.Spreadsheet.v24.2.Core.dll
NuGet Package: DevExpress.Spreadsheet.Core
Declaration
Property Value
Type | Description |
---|---|
Boolean | true to automatically change text size to fit a cell; otherwise, false. |
Remarks
If the ShrinkToFit property is enabled, the size of cell text changes as follows:
- If you add text to a cell or decrease the column width, cell text shrinks until cell content is completely visible.
- If you remove text from a cell or increase the column width, cell text size increases to the initial cell font size.
Note
The ShrinkToFit option does not change the Size property value of the cell font.
If cell text wraps into multiple lines (WrapText is true), the ShrinkToFit option does not apply.
The Spreadsheet offers you an alternative method to make sure a cell displays its content completely. Call the Column.AutoFit method to increase the column width instead of reducing text size.
Specify the ShrinkToFit Option in Code
Enable This Option for a Cell
The code sample below demonstrates how to specify the ShrinkToFit option for an individual cell. Use the cell’s Formatting.Alignment property to access and modify the Alignment object’s properties.
var cellB1 = worksheet.Cells["B1"];
cellB1.Value = "The Alignment.ShrinkToFit property is applied";
cellB1.Alignment.ShrinkToFit = true;
Enable This Option for a Cell Range
To specify alignment options for a cell range, modify the Alignment object’s properties within the CellRange.BeginUpdateFormatting—CellRange.EndUpdateFormatting method pair.
var rangeA1C3 = worksheet.Range["A1:C3"];
var rangeFormatting = rangeA1C3.BeginUpdateFormatting();
rangeFormatting.Alignment.ShrinkToFit = true;
rangeA1C3.EndUpdateFormatting(rangeFormatting);
You can also use cell styles to share the same alignment options between multiple cells. Refer to the following help topic for details on how to specify custom format settings for a cell style: How to: Create or Modify a Style.
Specify the ShrinkToFit Option in the Spreadsheet Control’s UI (WinForms/WPF)
Select a cell or cell range to which you want to apply the ShrinkToFit option. On the Home tab, click the dialog box launcher in the bottom-right corner of the Alignment group to invoke the Format Cells dialog.
Select the Shrink to fit checkbox to enable this option for selected cells.
Related GitHub Examples
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the ShrinkToFit property.
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.