Igor/10Tec
2020-04-24T14:20:48Z
A frequent question to our tech support service is how to specify a formatting for all cells in a column. I think it is a good idea to place an answer to this question in this KB forum section for iGrid ActiveX. Let me explain how to right-align cell values in a column. Other formatting options, such as color formatting, can be coded the similar way using the corresponding property of iGrid.
iGrid provides you with two ways to implement such a task.

The first and preferable way is to specify the alignment for the cells in the specified column BEFORE you create cells in it. This can be done with the column default cell object returned by the ColDefaultCell property of iGrid. The column default cell object stores the default values of all cell properties, and they are applied to all new cells that will be created in the column automatically. If you want to right-align values in the 1st column, you can use the following statement when you initialize your grid:
iGrid1.ColDefaultCell(1).eAlignH = igAlignHRight

Pay attention to the fact that this setting must be done before you add rows to your grid – for example, with the RowCount property:
iGrid1.AddCol
iGrid1.ColDefaultCell(1).eAlignH = igAlignHRight
iGrid1.RowCount = 5

The other way is to apply required formatting to the existing cells in the column AFTER you have created them. You can do that in a loop by setting the CellAlignH property to igAlignHRight:
Dim i As Long
For i = 1 To iGrid1.RowCount
   iGrid1.CellAlignH(i, 1) = igAlignHRight
Next

However, the latest iGrid provides you with the SetColCellsProp method for the same purpose. It is optimized for the scenario when you need to change a property for all cells in a column and it may work much faster than the loop above. Here is an equivalent statement:
iGrid1.SetColCellsProp 1, igPropCellAlignH, igAlignHRight

The last two optional parameters of the method allow you to limit the range of processed cells by the start and end row if it is required.

RECAP
Use the column default cell object to specify formatting for your cells everywhere where it is possible. This will eliminate the need in formatting operations after the cells were created, which results in the fastest execution of your code.