ComponentOne FlexGrid for WinForms
Using the C1FlexGrid Control / Data Annotation
In This Topic
    Data Annotation
    In This Topic

    Data annotation means adding meaningful metadata tags to the classes and other objects, making it easier to bridge the gap between models and views by performing data validation and displaying appropriate messages to the end users. For example, you may use data annotations to specify how items should be formatted, what their captions should be, whether they should be editable or not.

    FlexGrid supports multiple data annotation attributes used for customizing data classes, displaying data from source, and setting validation rules. To use the attributes in your project, you need to add a reference to the System.ComponentModel.DataAnnotations assembly and then add the attributes to your data objects in the code.

    Note: The C1FlexGrid supports the annotations defined in .NET 4. System.ComponentModel.DataAnnotations should be version or higher.

    Listed below are some of the major annotation attributes that are supported in FlexGrid control. Cilck here for complete list of DataAnnotation attributes.

    Attribute Name Functionality in FlexGrid
    Association Specifies that an entity member represents a data relationship, such as a foreign key relationship.
    Display Provides a general-purpose attribute that lets you specify localizable strings for types and members of entity partial classes.
    DisplayFormat Specifies how data fields are displayed and formatted by ASP.NET Dynamic Data.
    DisplayColumn Specifies the column that is displayed in the referred table as a foreign-key column.
    Editable Indicates whether a data field is editable.
    Key Denotes one or more properties that uniquely identify an entity.


    • RequiredAttribute
    • StringLengthAttribute
    • RangeAttribute
    • RegularExpressionAttribute
    • MinLengthAttribute
    • MetaDataAttribute
    • MaxLengthAttribute
    • EmailAddressAttribute
    • CompareAttribute
    • DataTypeAttribute
    The data annotation validation attributes are used as validation rules in FlexGrid operations.

    The following code example shows how data annotation feature works in FlexGrid control.

    // auto-generated CustomerName column header will show "Customer"
    // This column also requires non-empty string with minimal length at least 2 symbols.
    [Display(Name = "Customer")]
    [StringLength(int.MaxValue, MinimumLength = 2)]
    public string CustomerName { get; set; }
    // auto-generated CustomerID column will be invisible.
    [Display(AutoGenerateField = false)]
    public int CustomerID { get; set; }
    // auto-generated "Frequency" column will show values formatted as percentages
    // and will not allow editing.
    [DisplayFormat(DataFormatString = "0%")]
    public double Frequency { get; set; }
    // auto-generated "Age" column will allow values in the predefined
    // range.
    [Range(10, 90)]
    public int Age { get; set; }
    // create some sample data
    public static BindingList<Data> GetSampleData(int cnt)
        var list = new BindingList<Data>();
        var rnd = new Random();
        for (int i = 0; i < cnt; i++)
           var item = new Data();
           item.CustomerName = _firstNames[rnd.Next(0, _firstNames.Length)] + " " + _lastNames[rnd.Next(0, _lastNames.Length)];
           item.CustomerID = i;
           item.Frequency = rnd.NextDouble();
           item.Age = rnd.Next(10, 91);
           return list;

    Following image is the output of above code. In this example, CustomerName property from the dataset is replaced with metadata Customer. Also, StringLength attribute is used to specify the string length and validate the input value.