Cell Formatting in 10Tec WinForms Grid

(iGrid.NET Control Tour, Step 2 of 9)

Cell formatting options

In iGrid you can format each cell INDIVIDUALLY with the following options:

  • Set the background and foreground colors.
  • Each cell can have its own font.
  • The cell text can be left-aligned, right-aligned, or centered horizontally. The similar vertical alignment options are available as well.
  • The cell text can appear on a single line or multiple lines. It can be truncated with a trailing ellipsis (...) if it is bigger than the cell boundaries.
  • Cells can be indented so selection and drawing starts at the specified offset. You can control the left, top, right and bottom indent independently.
  • To get the text representation of the cell value, you can specify a .NET Framework format string or even use your own format provider based on the standard .NET IFormatProvider interface.

The screenshot below illustrates how you can use these formatting options:

cell formatting options (foreground and background color, font, alignment)

Pay attention to the fact that each row in iGrid.NET can have a different height.

Cell styles

One of the main distinctive features of our WinForms grid is the ability to use cell styles to format cells. This works like styles in Microsoft Word. If several cells use the same style object, they look and work the same way according to the properties of the style object. And you can change the look and behavior of all cells linked to one style object simply by changing the properties of the style object. This approach allows you to make your code simpler and provides excellent performance when you need to set formatting of hundreds of thousands of cells.

By default, a cell does not have its own cell style object and inherits the properties of the cell style object of the column it belongs to. You can use this to reformat the entire column in one statement, for example:

iGrid1.Cols[0].CellStyle.BackColor = Color.LightSkyBlue;
WinForms grid code sample: set column cell style backcolor

We can also create the required style as a standalone object and apply it to several columns to get the same formatting in those columns. As an example, let's format the values in the second and third columns as currency values with the thousand separator and align them to the right:

WinForms grid code sample: format column cells with a format string

First, we define the following cell style object in our form:

private iGCellStyle CurrencyCellStyle = new iGCellStyle();

Then we set the required style properties and apply this style to the 2nd and 3rd columns:

CurrencyCellStyle.TextAlign = iGContentAlignment.TopRight;
CurrencyCellStyle.FormatString = "{0:$#,##}";

iGrid1.Cols[1].CellStyle = CurrencyCellStyle;
iGrid1.Cols[2].CellStyle = CurrencyCellStyle;

That's all. Custom formats in iGrid.NET are specified as if you used the String.Format() method from the .NET Framework.

Formatting individual cells

If you need to format an individual cell, you can define a cell style object with required formatting and apply it to this cell. But there is a simpler way: you can format a cell with its properties. Look at this code:

iGrid1.Cells[1, 2].BackColor = Color.Magenta;
iGrid1.Cells[1, 2].ForeColor = Color.White;
iGrid1.Cells[1, 2].Font = new Font("Tahoma", 8, FontStyle.Bold);

We access the cell's properties directly to get the desired look:

WinForms grid code sample: format an individual cell with its properties

By default, all cell properties are set to a special NotSet value. It means that the value of the corresponding property is inherited from the cell style object of the column the cell belongs to. Continuing the previous sample, we could right-align the values in the last column using the column's cell style object, and the formatted cell in the 2nd row would accept this alignment setting:

iGrid1.Cols[2].CellStyle.TextAlign = iGContentAlignment.TopRight;
WinForms grid code sample: cell properties inheritance

If you need to format several cells using the same formatting, we recommend that you use a cell style object instead of setting the same properties for every cell. This approach simplifies your code, your efforts to support it in the future, and yields better performance in huge grids.

High DPI and tablet support »