The appearance of the cells (alignment, font, colors, borders, and so on) is handled with CellStyle objects. The grid has a Styles property that holds the collection of styles used to format the grid. This collection has some built-in members that define the appearance of grid elements, such as fixed and scrollable cells, selection, focus cell, and so on. You can change these styles to modify the way the grid looks, and you can also create your own custom styles and assign them to cells, rows, or columns.
Changing the built-in styles is the simplest way to change the appearance of the grid. For example, the code below displays the selection as bold green characters over a red background:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Dim cs As CellStyle = _flex.Styles.Highlight cs.Font = New Font(_flex.Font, FontStyle.Bold) cs.ForeColor = Color.Green cs.BackColor = Color.Red |
To write code in C#
C# |
Copy Code
|
---|---|
CellStyle cs = _flex.Styles.Highlight; cs.Font = new Font(_flex.Font, FontStyle.Bold); cs.ForeColor = Color.Green; cs.BackColor = Color.Red; |
You can also create your own styles and assign them to cells, rows and columns. For example, the code below creates a custom cell style and assigns it to every fifth row:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Dim cs As CellStyle = _flex.Styles.Add("Fifth") cs.BackColor = Color.Gray Dim idex% For idex = _flex.Rows.Fixed To _flex.Rows.Count - 1 Step 5 _flex.Rows(idex).Style = cs Next |
To write code in C#
C# |
Copy Code
|
---|---|
CellStyle cs = _flex.Styles.Add("Fifth"); cs.BackColor = Color.Gray; for (int index = _flex.Rows.Fixed ; index <= _flex.Rows.Count - 1; index += 5) { _flex.Rows[index].Style = cs; } |
Here's an example that shows how you can create custom styles and assign them to columns, rows, and cell ranges:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
' Create a new custom style Dim s As CellStyle = _flex.Styles.Add("MyStyle") s.BackColor = Color.Red s.ForeColor = Color.White ' Assign the new style to a column. _flex.Cols(3).Style = _flex.Styles("MyStyle") ' Assign the new style to a row. _flex.Rows(3).Style = _flex.Styles("MyStyle") ' Assign the new style to a cell range. Dim rg As CellRange = _flex.GetCellRange(4, 4, 6, 6) rg.Style = _flex.Styles("MyStyle") |
To write code in C#
C# |
Copy Code
|
---|---|
// Create a new custom style. CellStyle s = _flex.Styles.Add("MyStyle"); s.BackColor = Color.Red; s.ForeColor = Color.White; // Assign the new style to a column. _flex.Cols[3].Style = _flex.Styles["MyStyle"]; // Assign the new style to a row. _flex.Rows[3].Style = _flex.Styles["MyStyle"]; // Assign the new style to a cell range. CellRange rg = _flex.GetCellRange(4,4,6,6); rg.Style = _flex.Styles["MyStyle"]; |
If you prefer, you can set up styles at design time using the C1FlexGrid Style Editor instead of writing code to do it. For details on customizing cell appearance with the C1FlexGrid Style Editor, see C1FlexGrid Style Editor.