Skip to main content Skip to footer

New CellType for Spread for WinForms – II

In the last blog we discussed about one of the new celltypes introduced in Spread for WinForms. Here in this blog, let's discuss another newly added CellType for Spread for WinForms i.e. GcDateTimeCellType.


With GcDateTimeCellType you can set cell values in date and time format. GcDateTimeCellType also supports date calendar. You can allow the end users to pick a date from calendar by double clicking the GcDateTime cell. GcDateTimeCalendar Now you can access Time, Month , Day and Year field individually and make settings for them. You can make settings for different fields in a GcDateTimeCell when a cell is in edit mode and when the cell is not in edit mode. The cell uses "DisplayFields" to paint the cell when not in edit mode, and uses "Fields" to paint the cell when in edit mode. These are two DateField collections that represent the item in GcDateTime object.

Using GcDateTimeCellType

  1. To format Fields : You can use GcDateTimeCellType to format "Fields'. Fields are basically date fields which are shown in Spread cell when it is in edit mode. To format these fields, first you need to set Fields objects and then add them to the GcDateTimeCellType's Fields collection.

    //Create Field objects  
    GrapeCity.Win.Spread.InputMan.CellType.Fields.DateDayFieldInfo dayInfo = new GrapeCity.Win.Spread.InputMan.CellType.Fields.DateDayFieldInfo();  
    dayInfo.BackColor = Color.Lavender;  
    GrapeCity.Win.Spread.InputMan.CellType.Fields.DateMonthFieldInfo monthInfo = new GrapeCity.Win.Spread.InputMan.CellType.Fields.DateMonthFieldInfo();  
    monthInfo.BackColor = Color.Azure;  
    GrapeCity.Win.Spread.InputMan.CellType.Fields.DateYearFieldInfo yearInfo = new GrapeCity.Win.Spread.InputMan.CellType.Fields.DateYearFieldInfo();  
    yearInfo.BackColor = Color.Crimson;  
    GrapeCity.Win.Spread.InputMan.CellType.Fields.DateLiteralFieldInfo literalInfo= new GrapeCity.Win.Spread.InputMan.CellType.Fields.DateLiteralFieldInfo();  
    literalInfo.Text = "/";  
    //Add to GcDateTimeCell's Fields Collection  
    GrapeCity.Win.Spread.InputMan.CellType.GcDateTimeCellType gcDateTimecell= new GrapeCity.Win.Spread.InputMan.CellType.GcDateTimeCellType();  
  2. To format DisplayFields : Similarly you can format DisplayFields for GcDateTimeCellType. DisplayFields are date fields shown when the cell is not in edit mode. For example if the date cell is not in edit mode we can set formatting like showing leading zeroes, Backcolor, Forecolor etc. for Day, Month, Literal and Year fields.

    //Create Field objects  
    GrapeCity.Win.Spread.InputMan.CellType.Fields.DateDayDisplayFieldInfo day = new GrapeCity.Win.Spread.InputMan.CellType.Fields.DateDayDisplayFieldInfo();  
    day.ShowLeadingZero = true;  
    GrapeCity.Win.Spread.InputMan.CellType.Fields.DateMonthDisplayFieldInfo month = new GrapeCity.Win.Spread.InputMan.CellType.Fields.DateMonthDisplayFieldInfo();  
    month.ShowLeadingZero = true;  
    GrapeCity.Win.Spread.InputMan.CellType.Fields.DateYearDisplayFieldInfo year = new GrapeCity.Win.Spread.InputMan.CellType.Fields.DateYearDisplayFieldInfo();  
    year.ShowLeadingZero = true;  
    GrapeCity.Win.Spread.InputMan.CellType.Fields.DateLiteralDisplayFieldInfo lday = new GrapeCity.Win.Spread.InputMan.CellType.Fields.DateLiteralDisplayFieldInfo();  
    lday.Text = "/";  
    //Add to GcDateTimeCell's DisplayFields Collection  
    GrapeCity.Win.Spread.InputMan.CellType.GcDateTimeCellType gcDateTimecell= new GrapeCity.Win.Spread.InputMan.CellType.GcDateTimeCellType();  

    Have a look at the image shown below with the formatted fields using above code: FormattedFieldsGcDateTime

  3. ValidateMode Property : Another important feature of this celltype is that it can validate the user input by just setting the ValidateMode property. There are three enumerations available for this property:

    • GrapeCity.Win.Spread.InputMan.CellType.ValidateModeEx.Validate
    • GrapeCity.Win.Spread.InputMan.CellType.ValidateModeEx.ValidateEx
    • GrapeCity.Win.Spread.InputMan.CellType.ValidateModeEx.ValidateNone

    Where one is self explanatory, with ValidationMode set to "Validate" the validation happens with no strict rules and the values(dates) with no existence in real are also allowed with the same format. However if the ValidationMode is set to "ValidateEx" it performs a strict validation and validator becomes smarter so now the dates which are not real for example 31st Feb are not allowed. Please have a look at the code snippet below which shows how to use this in code:

    GrapeCity.Win.Spread.InputMan.CellType.GcDateTimeCellType datecell = new GrapeCity.Win.Spread.InputMan.CellType.GcDateTimeCellType();  
    datecell.ValidateMode = GrapeCity.Win.Spread.InputMan.CellType.ValidateModeEx.ValidateEx;  
    fpSpread1.Sheets[0].Cells[0, 0].CellType = datecell;

Download C# Sample Download VB.Net Sample


comments powered by Disqus