GrapeCity MultiRow Windows Forms Documentation
Tag

You can save specific values inside the style tag. For example, you can save hidden data and read and write it at runtime. The tag can be set using the CellStyle.Tag property. The Styles for each of the Cell Types can be used to determine if the cell type supports the style tag.

Using the Designer

  1. Select the cells where you want to set the tag (for example: textBoxCell1).
  2. Set the desired value in the textBoxCell1.Style.Tag in the Properties window.

The value set in the CellStyle.Tag property is not displayed in the cell. This allows you to to save information that you do not want to display, or for extended data. Since the Tag property is of the type Object, it can save information of any type.

Using Code

The following code sets the upper limit on the number of characters in the Tag property, which is then used in the validation of values.

[VB]

Imports GrapeCity.Win.MultiRow

Private Sub Form1_Load( _
    ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles MyBase.Load
    Dim template As Template
    template = GrapeCity.Win.MultiRow.Template.Default
    template.Row.Cells(0).Style.BackColor = Color.Azure
    template.Row.Cells(0).Style.Tag = 5

    GcMultiRow1.Template = template
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("Number of characters exceeds maximum limit of characters :{0}.", maxLength)
            Else
                gcMultiRow(e.RowIndex, e.CellIndex).ErrorText = ""
            End If
        End If
    End If
End Sub

[CS]

using GrapeCity.Win.MultiRow; 

private void Form1_Load(object sender, EventArgs e)
{
    Template template = GrapeCity.Win.MultiRow.Template.Default;
    template.Row.Cells[0].Style.BackColor = Color.Azure;
    template.Row.Cells[0].Style.Tag = 5;

    gcMultiRow1.Template = template;
}

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("Number of characters exceeds maximum limit of characters :{0}.", maxLength);
            }
            else
            {
                gcMultiRow[e.RowIndex, e.CellIndex].ErrorText = "";
            }
        }
    }
}

Cell and Tag Conditional Styles

Since the conditional cell style (ConditionalCellStyle) and named cell style (NamedCellStyle) do not have the CellStyle.Tag property, there is a limitation when using the Tag property of each cell when also using these styles. To use the Tag property of each cell while using these styles, the following workaround can be used. You can use arrays or collections to save values for each cell since the Tag property can save values of any type.

Using Code

This example sets the Tag property.

[VB]

GcMultiRow1.Rows(0).Tag = New String() { "Value of Cell1", "Value of Cell2" }

[CS]

gcMultiRow1.Rows[0].Tag = new string[] { "Value of Cell1", "Value of Cell2" };

Since the combined cell style (CombinedCellStyle) can hold other cell styles, you can use the normal style (CellStyle) and other cell styles on one cell. It is not very effective to use this method just for the Tag property, so the Row.Tag property is recommended.

Using Code

This example assigns a style.

[VB]

Imports GrapeCity.Win.MultiRow

' In case of directly using the conditional styles
Dim conditionalCellStyle1 As New ConditionalCellStyle()
GcMultiRow1.CurrentCell.Style = conditionalCellStyle1

' In case of using conditional styles together with combined cell styles
Dim cellStyle1 As New CellStyle()
cellStyle1.Tag = "Desired Value"
Dim conditionalCellStyle1 As New ConditionalCellStyle()
Dim combinedCellStyle1 As New CombinedCellStyle()
combinedCellStyle1.Items.Add(cellStyle1)
combinedCellStyle1.Items.Add(conditionalCellStyle1)
GcMultiRow1.CurrentCell.Style = combinedCellStyle1

[CS]

using GrapeCity.Win.MultiRow;

// In case of directly using the conditional styles
ConditionalCellStyle conditionalCellStyle1 = new ConditionalCellStyle();
gcMultiRow1.CurrentCell.Style = conditionalCellStyle1;

// In case of using conditional styles together with combined cell styles
CellStyle cellStyle1 = new CellStyle();
cellStyle1.Tag = "Desired Value";
ConditionalCellStyle conditionalCellStyle1 = new ConditionalCellStyle();
CombinedCellStyle combinedCellStyle1 = new CombinedCellStyle();
combinedCellStyle1.Items.Add(cellStyle1);
combinedCellStyle1.Items.Add(conditionalCellStyle1);
gcMultiRow1.CurrentCell.Style = combinedCellStyle1;
See Also

 

 


Copyright © GrapeCity, inc. All rights reserved.

Support Options