# Aligning Cell Contents

Learn how to set cell alignment properties in Spread Designer for effective data presentation.

## Content

You can determine how the contents are aligned in a cell or in a group of cells. In code, simply set the [HorizontalAlignment](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Cell.HorizontalAlignment.html) property and the [VerticalAlignment](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Cell.VerticalAlignment.html) properties, and make use of the [CellHorizontalAlignment](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.CellHorizontalAlignment.html) and [CellVerticalAlignment](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.CellVerticalAlignment.html) enumerations. In the Spread Designer, set those properties accordingly. The following figure shows the result of the code in the first example.
![Cell Align Example](https://cdn.mescius.io/document-site-files/images/2238e618-a1fb-45a6-9ce5-9a4157bd2931/images/cell-align-labeled.png)
There are additional properties in the [Cell](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Cell.html) class such as [TextIndent](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Cell.TextIndent.html) and [CellPadding](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Cell.CellPadding.html) that can be used to create extra margins around cell text.
For an explanation of how the alignment affects the overflow of data, refer to [Allowing Cell Data to Overflow](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-ssobject-cell/spwin-celldata/spwin-celloflow)
You can keep the cell alignment when editing with the [AllowEditorVerticalAlign](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.CellType.EditBaseCellType.AllowEditorVerticalAlign.html) property.

## Using the Properties Window

1. At design time, in the **Properties** window, select the Spread component.
2. Select the **Sheets** property.
3. Click the button to display the **SheetView Collection Editor**.
4. In the **Members** list, select the sheet in which the cells appear.
5. In the properties list, select the **Cells** property and then click the button to display the **Cell, Column, and Row Editor**.
6. Select the cells for which you want to set the alignment.
7. In the properties list, select the **HorizontalAlignment** property and choose the alignment.
8. To set the vertical alignment, select the **VerticalAlignment** property in the properties list and choose the alignment.
9. Click **OK** to close the **Cell, Column, and Row Editor**.
10. Click **OK** to close the **SheetView Collection Editor**.

## Using a Shortcut

Set the [HorizontalAlignment](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Cell.HorizontalAlignment.html) property and the [VerticalAlignment](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Cell.VerticalAlignment.html) properties for the Cells shortcut object.
**Example**
This example code sets the horizontal alignment of the first cell (A1) to be right-aligned, the vertical alignment of that cell to be bottom-aligned, and the horizontal alignment and vertical alignment of cells from B2 to C3 to be centered. The preceding figure illustrates the results.

```csharp
fpSpread1.Sheets[0].Cells[0,0].HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Right;
fpSpread1.Sheets[0].Cells[0,0].VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Bottom;
fpSpread1.Sheets[0].Cells[1,1,2,2].HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Center;
fpSpread1.Sheets[0].Cells[1,1,2,2].VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Center;
```

```vbnet
FpSpread1.Sheets(0).Cells(0,0).HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Right
FpSpread1.Sheets(0).Cells(0,0).VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Bottom
FpSpread1.Sheets(0).Cells(1,1,2,2).HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Center
fpSpread1.Sheets(0).Cells(1,1,2,2).VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Center
```

## Using Code

Set the [HorizontalAlignment](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Cell.HorizontalAlignment.html) property and the [VerticalAlignment](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Cell.VerticalAlignment.html) properties for the [Cell](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Cell.html) object.
**Example**
This example code sets the horizontal alignment for a specific cell and the vertical alignment for a range of cells.

```csharp
FarPoint.Win.Spread.Cell cellA1;
cellA1 = fpSpread1.ActiveSheet.Cells[0, 0];
cellA1.HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Right;
cellA1.VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Bottom;
FarPoint.Win.Spread.Cell cellrange;
cellrange = fpSpread1.ActiveSheet.Cells[1,1,2,2];
cellrange.HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Center;
cellrange.VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Center;
```

```vbnet
Dim cellA1 As FarPoint.Win.Spread.Cell
cellA1 = FpSpread1.ActiveSheet.Cells(0, 0)
cellA1.HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Right
cellA1.VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Bottom
Dim cellrange As FarPoint.Win.Spread.Cell
cellrange = FpSpread1.ActiveSheet.Cells(1, 1, 2, 2)
cellrange.HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Center
cellrange.VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Center
```

## Using the Spread Designer

1. In the work area, select the cell or cells for which you want to set the horizontal alignment of the cell contents.
2. In the properties list (in the **Misc** category), select the **HorizontalAlignment** property.
3. Click the drop-down button to display the choices and choose the alignment.
4. Repeat these steps and in the properties list, select **VerticalAlignment** to set the vertical alignment of the cell or cells.
5. From the **File** menu choose **Apply and Exit** to apply your changes to the component and exit Spread Designer.

## Center Across Selection

You can center-align the contents of the selected cells across columns without merging the cells. This type of horizontal alignment can be set using the **HorizontalAlignment.CenterAcrossSelection** enumeration option from the **GrapeCity.Spreadsheet** namespace.
In order to implement the **CenterAcrossSelection** alignment option, the **AllowCellOverflow** property of **FpSpread** class must be set to true and the **BorderCollapse** property must be set to ‘Enhanced’.
![centerAcrossSelection](https://cdn.mescius.io/document-site-files/images/2238e618-a1fb-45a6-9ce5-9a4157bd2931/images/centeracrossselection3.png)
This example code sets the center across selection for cells:

```csharp
fpSpread1.AllowCellOverflow = true;
fpSpread1.BorderCollapse = BorderCollapse.Enhanced;
IWorksheet TestActiveSheet = fpSpread1.AsWorkbook().ActiveSheet;
TestActiveSheet.Cells["C2"].Text = "test feature setting";
TestActiveSheet.Cells["C2:K2"].HorizontalAlignment = GrapeCity.Spreadsheet.HorizontalAlignment.CenterAcrossSelection;//set for big range
TestActiveSheet.Cells["C3"].Text = "test feature setting";
TestActiveSheet.Cells["C3"].HorizontalAlignment = GrapeCity.Spreadsheet.HorizontalAlignment.CenterAcrossSelection;//set for 1 cell          
TestActiveSheet.Cells["C4"].Text = "test feature setting";
TestActiveSheet.Cells["C4:D4"].HorizontalAlignment = GrapeCity.Spreadsheet.HorizontalAlignment.CenterAcrossSelection;//set for small range start from data cell         
TestActiveSheet.Cells["C5"].Text = "test feature setting";
TestActiveSheet.Cells["B5:C5"].HorizontalAlignment = GrapeCity.Spreadsheet.HorizontalAlignment.CenterAcrossSelection;//set for small range end by data cell         
FarPoint.Win.Spread.Dialogs.BuiltInDialogs.FormatCells(TestActiveSheet.Cells["A1:D5"]).ShowDialog();
```

```vbnet
FpSpread1.AllowCellOverflow = True
FpSpread1.BorderCollapse = BorderCollapse.Enhanced
IWorksheet TestActiveSheet = FpSpread1.AsWorkbook().ActiveSheet
TestActiveSheet.Cells("C2").Text = "test feature setting"
TestActiveSheet.Cells("C2:K2").HorizontalAlignment = GrapeCity.Spreadsheet.HorizontalAlignment.CenterAcrossSelection ' set for big range
TestActiveSheet.Cells("C3").Text = "test feature setting"
TestActiveSheet.Cells("C3").HorizontalAlignment = GrapeCity.Spreadsheet.HorizontalAlignment.CenterAcrossSelection ' set for 1 cell   
TestActiveSheet.Cells("C4").Text = "test feature setting"
TestActiveSheet.Cells("C4:D4").HorizontalAlignment = GrapeCity.Spreadsheet.HorizontalAlignment.CenterAcrossSelection ' set for small range start from data cell
TestActiveSheet.Cells("C5").Text = "test feature setting"
TestActiveSheet.Cells("B5:C5").HorizontalAlignment = GrapeCity.Spreadsheet.HorizontalAlignment.CenterAcrossSelection ' set for small range end by data cell
FarPoint.Win.Spread.Dialogs.BuiltInDialogs.FormatCells(TestActiveSheet.Cells("A1:D5")).ShowDialog()
```

## Using the Spread Designer

1. In the work area, select the range of cells for which you want to set the center across selection.
2. Select the **Spread** option from the selected item list of the properties window.
3. Select the **BorderCollapse** property from the properties list of the **Appearance** category. Set the property to Enhanced.
4. Select the **AllowCellOverflow** property from the properties list of the **Behavior** category and set it to true.
5. Click the Dialog Box Launcher arrow at the bottom right corner of the **Alignment** group of **Home** menu. This opens the **Format Cells** dialog.
<br>
    ![](https://cdn.mescius.io/document-site-files/images/2238e618-a1fb-45a6-9ce5-9a4157bd2931/images/gif-for-center-across-select.gif)
6. Select the **Center Across Selection** from the dropdown list of the Horizontal option of **Alignment** tab in the dialog.
7. Click **OK** to apply the changes.

## See Also

[Resizing the Data to Fit the Cell](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-ssobject-cell/spwin-celldata/spwin-appearshrink)
[Allowing Cell Data to Overflow](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-ssobject-cell/spwin-celldata/spwin-celloflow)
[Resizing a Cell to Fit the Data](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-ssobject-cell/spwin-celldata/spwin-cellfitdata)