Skip to main content Skip to footer

How to define a selected cell background color when SelectionUnit is Row/Column in WinForms

Issue:

Using Spread for WinForms, if the SelectionUnit is either Row/Column, the background color of active cell in the selection appears different.
Therefore, this article shows how to fill the current active cell with the same background color as of the rest of the selection.

Resolution:

There are two common approaches for painting active/selected cells:

Approach 1:  The active cell is painted with the normal cell colors while all other selected cells are painted with the selected color. This is the way that Excel and Spread paint.

Approach 2: All selected cells (including the active cell) is painted with the selected colors. This is the way that OpenOffice paints.

There is no good way to emulate approach 2 in Spread. Therefore, we need to define uniform selection color for the whole selected unit and handle the LeaveCell event.

Color selectColor;
private void Form1_Load(object sender, EventArgs e)
{
            selectColor = Color.Aqua;
            fpSpread1.ActiveSheet.SelectionStyle = SelectionStyles.SelectionColors;
            fpSpread1.ActiveSheet.SelectionPolicy = FarPoint.Win.Spread.Model.SelectionPolicy.Single;
            fpSpread1.ActiveSheet.SelectionBackColor = selectColor;
            fpSpread1.ActiveSheet.SelectionUnit = SelectionUnit.Row;
            fpSpread1.LeaveCell += FpSpread1_LeaveCell;
}
private void FpSpread1_LeaveCell(object sender, LeaveCellEventArgs e)
{
            fpSpread1.ActiveSheet.Cells[e.Row, e.Column].BackColor = Color.White;
            fpSpread1.ActiveSheet.Cells[e.NewRow, e.NewColumn].BackColor = selectColor;
}

Before:

After:

Tags:

Ruchir Agarwal