テンプレートに名前付きセルスタイルを登録するには、
Template.NamedCellStylesプロパティを使用します。次のコードは、「寒色」という名前で背景色が青のセルスタイルを追加します。
Imports GrapeCity.Win.MultiRow
' 背景色が青のセルスタイルを作成する
Dim blueCellStyle As New CellStyle()
blueCellStyle.BackColor = Color.Blue
Dim Template1 As Template = template.Default
' セルスタイルを「寒色」という名前で登録する
Template1.NamedCellStyles.Add("寒色", blueCellStyle)
GcMultiRow1.Template = Template1
GcMultiRow1.Rows(0).Cells(0).Style = New NamedCellStyle("寒色")
using GrapeCity.Win.MultiRow;
// 背景色が青のセルスタイルを作成する
CellStyle blueCellStyle = new CellStyle();
blueCellStyle.BackColor = Color.Blue;
Template template1 = Template.Default;
// セルスタイルを「寒色」という名前で登録する
template1.NamedCellStyles.Add("寒色", blueCellStyle);
gcMultiRow1.Template = template1;
gcMultiRow1.Rows[0].Cells[0].Style = new NamedCellStyle("寒色");
テンプレートに登録した名前付きセルスタイルは、GcMultiRowコントロールにテンプレートを設定したときに自動的にGcMultiRowコントロールで有効になります。
GcMultiRowコントロールでは、テンプレートで設定した名前付きセルスタイルを変更できます。次のコードは、「寒色」という名前で登録した名前付きセルスタイルを、淡い色の新しいセルスタイルで置き換えます。
Imports GrapeCity.Win.MultiRow
Dim blueCellStyle As New CellStyle()
blueCellStyle.BackColor = Color.Blue
Dim Template1 As Template = Template.Default
Template1.NamedCellStyles.Add("寒色", blueCellStyle)
GcMultiRow1.Template = Template1
GcMultiRow1.Rows(0).Cells(0).Style = New NamedCellStyle("寒色")
Dim lightblueCellStyle As New CellStyle
lightblueCellStyle.BackColor = Color.LightBlue
GcMultiRow1.NamedCellStyles.Remove("寒色")
GcMultiRow1.NamedCellStyles.Add("寒色", lightblueCellStyle)
using GrapeCity.Win.MultiRow;
CellStyle blueCellStyle = new CellStyle();
blueCellStyle.BackColor = Color.Blue;
Template template1 = Template.Default;
template1.NamedCellStyles.Add("寒色", blueCellStyle);
gcMultiRow1.Template = template1;
gcMultiRow1.Rows[0].Cells[0].Style = new NamedCellStyle("寒色");
CellStyle lightblueCellStyle = new CellStyle();
lightblueCellStyle.BackColor = Color.LightBlue;
gcMultiRow1.NamedCellStyles.Remove("寒色");
gcMultiRow1.NamedCellStyles.Add("寒色", lightblueCellStyle);
任意のセルに名前付きセルスタイルを適用するには、
Cell.StyleプロパティにNamedCellStyleオブジェクトを設定します。
Imports GrapeCity.Win.MultiRow
Dim blueCellStyle As New CellStyle()
blueCellStyle.BackColor = Color.Blue
Dim Template1 As Template = template.Default
Template1.NamedCellStyles.Add("寒色", blueCellStyle)
' template.Row.Cells(0).Style = New NamedCellStyle("寒色")
GcMultiRow1.Template = Template1
GcMultiRow1.Rows(0).Cells(0).Style = New NamedCellStyle("寒色")
using GrapeCity.Win.MultiRow;
CellStyle blueCellStyle = new CellStyle();
blueCellStyle.BackColor = Color.Blue;
Template template1 = Template.Default;
template1.NamedCellStyles.Add("寒色", blueCellStyle);
// template1.Row.Cells[0].Style = new NamedCellStyle("寒色");
gcMultiRow1.Template = template1;
gcMultiRow1.Rows[0].Cells[0].Style = new NamedCellStyle("寒色");
名前付きセルスタイルの登録と、セルへの名前付きセルスタイルの設定は前後しても問題ありません。次のコードは、セルへ名前付きセルスタイルを指定した後で名前付きセルスタイルを登録します。
Imports GrapeCity.Win.MultiRow
Dim Template1 As Template = template.Default
GcMultiRow1.Template = Template1
GcMultiRow1.Rows(0).Cells(0).Style = New NamedCellStyle("寒色")
Dim blueCellStyle As New CellStyle()
blueCellStyle.BackColor = Color.Blue
GcMultiRow1.NamedCellStyles.Add("寒色", blueCellStyle)
using GrapeCity.Win.MultiRow;
Template template1 = Template.Default;
gcMultiRow1.Template = template1;
gcMultiRow1.Rows[0].Cells[0].Style = new NamedCellStyle("寒色");
CellStyle blueCellStyle = new CellStyle();
blueCellStyle.BackColor = Color.Blue;
gcMultiRow1.NamedCellStyles.Add("寒色", blueCellStyle);
名前付きセルスタイルは、1つのテンプレートまたはコントロール内で一意です。既に使用されている名前付きセルスタイルを
Template.NamedCellStylesプロパティのコレクションに追加しようとすると、ArgumentExceptionが発生します。
名前付きセルスタイルが既に登録されているか確認するには、
NamedCellStyleDictionary.ContainsKeyメソッドを使用します。
If Template1.NamedCellStyles.ContainsKey("寒色") Then
Console.WriteLine("既に登録されています")
End If
if (template1.NamedCellStyles.ContainsKey("寒色"))
{
Console.WriteLine("既に登録されています");
}
名前付きセルスタイルを削除するには、
NamedCellStyleDictionary.Removeメソッドを実行します。削除した名前付きセルスタイルを使用していたセルでは、既定のスタイルが使用されます。この動作を明示的に変更するには、結合セルスタイルを使用します。