CellStyle.Tagプロパティに設定した値はセルに表示されません。このため、非表示の情報や拡張データの格納に適しています。TagプロパティはObject型のため、任意の型の情報を保持できます。
次のコードは、Tagプロパティに文字数の上限を保存し、値の検証時に利用します。
Imports GrapeCity.Win.MultiRow
Private Sub Form1_Load( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim Template1 As Template
Template1 = GrapeCity.Win.MultiRow.Template.Default
Template1.Row.Cells(0).Style.BackColor = Color.Azure
Template1.Row.Cells(0).Style.Tag = 5
GcMultiRow1.Template = Template1
End Sub
Private Sub GcMultiRow1_CellValidating( _
ByVal sender As System.Object, _
ByVal e As CellValidatingEventArgs _
) Handles GcMultiRow1.CellValidating
Dim gcMultiRow As GcMultiRow = TryCast(sender, GcMultiRow)
Dim cellStyle As CellStyle = gcMultiRow(e.RowIndex, e.CellIndex).Style
If cellStyle.Tag IsNot Nothing Then
Dim maxLength As Integer = CType(cellStyle.Tag, Integer)
If e.FormattedValue IsNot Nothing Then
Dim value As String = CType(e.FormattedValue, String)
If (value.Length > maxLength) Then
gcMultiRow(e.RowIndex, e.CellIndex).ErrorText = _
String.Format("{0} 文字以下で入力してください。", maxLength)
Else
gcMultiRow(e.RowIndex, e.CellIndex).ErrorText = ""
End If
End If
End If
End Sub
using GrapeCity.Win.MultiRow;
private void Form1_Load(object sender, EventArgs e)
{
Template template1 = GrapeCity.Win.MultiRow.Template.Default;
template1.Row.Cells[0].Style.BackColor = Color.Azure;
template1.Row.Cells[0].Style.Tag = 5;
gcMultiRow1.Template = template1;
}
private void gcMultiRow1_CellValidating(object sender, CellValidatingEventArgs e)
{
GcMultiRow gcMultiRow = sender as GcMultiRow;
CellStyle cellStyle = gcMultiRow[e.RowIndex, e.CellIndex].Style;
int maxLength = (int)cellStyle.Tag;
if (cellStyle.Tag != null)
{
int maxLength = (int)cellStyle.Tag;
if (e.FormattedValue != null)
{
string value = (string)e.FormattedValue;
if (value.Length > maxLength)
{
gcMultiRow[e.RowIndex, e.CellIndex].ErrorText =
string.Format("{0} 文字以下で入力してください。", maxLength);
}
else
{
gcMultiRow[e.RowIndex, e.CellIndex].ErrorText = "";
}
}
}
}
条件付きスタイル(ConditionalCellStyle)や名前付きセルスタイル(NamedCellStyle)にはCellStyle.Tagプロパティのような機能が提供されていないため、これらのセルスタイルを使用する場合にはセル単位のTagプロパティを使用できない制約が生じます。これらのセルスタイルを使用する場合にセル単位のTagプロパティを使用するには、次のような代替策があります。
Tagプロパティには任意の型の値を格納できるため、配列やコレクションを使用することでセル単位の値を格納できます。
GcMultiRow1.Rows(0).Tag = New String() { "セル1の値", "セル2の値" }
gcMultiRow1.Rows[0].Tag = new string[] { "セル1の値", "セル2の値" };
結合セルスタイル(CombinedCellStyle)は、他のセルスタイルを内包できるため、通常のセルスタイル(CellStyle)とその他のセルスタイルを1つのセルで併用できます。
ただし、Tagプロパティのためだけにこの方法を使用するのは効率的ではないため、通常はRow.Tagプロパティの使用をお勧めします。
Imports GrapeCity.Win.MultiRow
' 条件付きスタイルを直接使用する場合
Dim ConditionalCellStyle1 As New ConditionalCellStyle()
GcMultiRow1.CurrentCell.Style = ConditionalCellStyle1
' 条件付きスタイルを結合セルスタイルと共に使用する場合
Dim CellStyle1 As New CellStyle()
CellStyle1.Tag = "任意の値"
Dim ConditionalCellStyle1 As New ConditionalCellStyle()
Dim CombinedCellStyle1 As New CombinedCellStyle()
CombinedCellStyle1.Items.Add(CellStyle1)
CombinedCellStyle1.Items.Add(ConditionalCellStyle1)
GcMultiRow1.CurrentCell.Style = CombinedCellStyle1
using GrapeCity.Win.MultiRow;
// 条件付きスタイルを直接使用する場合
ConditionalCellStyle conditionalCellStyle1 = new ConditionalCellStyle();
gcMultiRow1.CurrentCell.Style = conditionalCellStyle1;
// 条件付きスタイルを結合セルスタイルと共に使用する場合
CellStyle cellStyle1 = new CellStyle();
cellStyle1.Tag = "任意の値";
ConditionalCellStyle conditionalCellStyle1 = new ConditionalCellStyle();
CombinedCellStyle combinedCellStyle1 = new CombinedCellStyle();
combinedCellStyle1.Items.Add(cellStyle1);
combinedCellStyle1.Items.Add(conditionalCellStyle1);
gcMultiRow1.CurrentCell.Style = combinedCellStyle1;