GrapeCity MultiRow Windows Forms Documentation
Manage Cell Styles with Names

You can manage styles with names if you use named cell styles (NamedCellStyle class). These styles are convenient when you want to develop styles based on sharing, re-using, or using them interchangeably like skins. In the MultiRow designer, you are supplied with the Template - Named Cell Styles 7.0 in order to be able to use and manage the named cell styles. Named cell styles can be used in both templates and GcMultiRow controls.

Register Named Cell Styles

Use the Template.NamedCellStyles property to register the named cell styles in the template.

Using Code

The following code adds a cell style "Cool Color" with blue as the backcolor.

[VB]

Imports GrapeCity.Win.MultiRow

' Create cell style that has blue as the backcolor
Dim blueCellStyle As New CellStyle()
blueCellStyle.BackColor = Color.Blue

Dim template1 As Template = template.Default
' Register the cell style with a name "Cool color"
template1.NamedCellStyles.Add("Cool color", blueCellStyle)

GcMultiRow1.Template = template1
GcMultiRow1.Rows(0).Cells(0).Style = New NamedCellStyle("Cool color")

[CS]

using GrapeCity.Win.MultiRow;

// Create cell style that has blue as the backcolor
CellStyle blueCellStyle = new CellStyle();
blueCellStyle.BackColor = Color.Blue;

Template template1 = Template.Default;
// Register the cell style with a name "Cool color"
template1.NamedCellStyles.Add("Cool color", blueCellStyle);

gcMultiRow1.Template = template1;
gcMultiRow1.Rows[0].Cells[0].Style = new NamedCellStyle("Cool color");

The named cell style registered in the template is automatically enabled in the GcMultiRow control, when the template is set into the GcMultiRow control. You can change the named cell style in the GcMultiRow control template.

Using Code

The following code replaces the cell style that has been registered with the name "Cool color" to a cell style which is light blue.

[VB]

Imports GrapeCity.Win.MultiRow

Dim blueCellStyle As New CellStyle()
blueCellStyle.BackColor = Color.Blue

Dim template1 As Template = Template.Default
template1.NamedCellStyles.Add("Cool color", blueCellStyle)

GcMultiRow1.Template = template1
GcMultiRow1.Rows(0).Cells(0).Style = New NamedCellStyle("Cool color")

Dim lightblueCellStyle As New CellStyle
lightblueCellStyle.BackColor = Color.LightBlue

GcMultiRow1.NamedCellStyles.Remove("Cool color")
GcMultiRow1.NamedCellStyles.Add("LightBlueColor", lightblueCellStyle)

[CS]

using GrapeCity.Win.MultiRow;

CellStyle blueCellStyle = new CellStyle();
blueCellStyle.BackColor = Color.Blue;

Template template1 = Template.Default;
template1.NamedCellStyles.Add("Cool color", blueCellStyle);

gcMultiRow1.Template = template1;
gcMultiRow1.Rows[0].Cells[0].Style = new NamedCellStyle("Cool color");

CellStyle lightblueCellStyle = new CellStyle();
lightblueCellStyle.BackColor = Color.LightBlue;

gcMultiRow1.NamedCellStyles.Remove("Cool color");
gcMultiRow1.NamedCellStyles.Add("LightBlueColor", lightblueCellStyle);

Using Named Cell Styles

To set a named cell style in a specific cell, you need to set the NamedCellStyle object in the Cell.Style property.

Using Code

This example uses a NamedCellStyle object.

[VB]

Imports GrapeCity.Win.MultiRow

Dim blueCellStyle As New CellStyle()
blueCellStyle.BackColor = Color.Blue

Dim template1 As Template = template.Default
template1.NamedCellStyles.Add("Cool color", blueCellStyle)
' template.Row.Cells(0).Style = New NamedCellStyle("Cool color")

GcMultiRow1.Template = template1
GcMultiRow1.Rows(0).Cells(0).Style = New NamedCellStyle("Cool color")

[CS]

using GrapeCity.Win.MultiRow;

CellStyle blueCellStyle = new CellStyle();
blueCellStyle.BackColor = Color.Blue;

Template template1 = Template.Default;
template1.NamedCellStyles.Add("Cool color", blueCellStyle);
// template1.Row.Cells[0].Style = new NamedCellStyle("Cool color");

gcMultiRow1.Template = template1;
gcMultiRow1.Rows[0].Cells[0].Style = new NamedCellStyle("Cool color");

The order of registering the named cell style and assigning it to a cell does not make a difference.

Using Code

The following code registers the named cell style after applying it to a cell.

[VB]

Imports GrapeCity.Win.MultiRow

Dim template1 As Template = template.Default
GcMultiRow1.Template = template1

GcMultiRow1.Rows(0).Cells(0).Style = New NamedCellStyle("Cool color")

Dim blueCellStyle As New CellStyle()
blueCellStyle.BackColor = Color.Blue
GcMultiRow1.NamedCellStyles.Add("Cool color", blueCellStyle)

[CS]

using GrapeCity.Win.MultiRow;

Template template1 = Template.Default;
gcMultiRow1.Template = template1;

gcMultiRow1.Rows[0].Cells[0].Style = new NamedCellStyle("Cool color");

CellStyle blueCellStyle = new CellStyle();
blueCellStyle.BackColor = Color.Blue;
gcMultiRow1.NamedCellStyles.Add("Cool color", blueCellStyle);

Check Whether the Named Cell Style is Registered

The named cell style is unique in a template or in a control. An ArgumentException occurs when you try to add an already existing named cell style to the Template.NamedCellStyles property collection. Use the NamedCellStyleDictionary.ContainsKey method to confirm whether the named cell style is already registered.

Using Code

This example uses the ContainsKey method.

[VB]

If template1.NamedCellStyles.ContainsKey("Cool color") Then
    Console.WriteLine("Already registered")
End If

[CS]

if (template1.NamedCellStyles.ContainsKey("Cool color"))
{
    Console.WriteLine("Already registered");
}

Delete Named Cell Styles

You can delete the named cell style with the NamedCellStyleDictionary.Remove method. The default style will be used in cells where that named cell style was applied. Use the combined cell style to change this behavior explicitly.

See Also

 

 


Copyright © GrapeCity, inc. All rights reserved.

Support Options