Skip to main content Skip to footer

Spread ASP.NET and Custom Cell Types

Spread ASP.NET allows you to create your own custom cell types. This can be useful if a default cell type does not have the feature you need. You can implement your own types of cells by creating a subclass that inherits an existing cell type (that is, override methods in that class). The custom cell type class should be marked as serializable so it works for the view state. The BaseCellType class is a class from which the built-in text based cell types (for example, general, text, number, data-time, and so on.) are derived. The class can also be used to derive custom cell types that are text based. The data model can hold any value, including colors. The cell type is always passed the raw value from the data model. You can override any of the following methods to create a custom cell. BaseCellType methods:

Clone

Creates a new copy of the object using the MemberwiseClone method.

Format

When deriving a cell type based on this type, override this to change what gets passed back and formatted as a string into the cell on the sheet from the object in the data model.

GetEditorControl

Gets the control used to edit the cell.

GetEditorValue

Gets the value from the editing control used for the cell.

GetPreferredSize

Gets the preferred (maximum required) size of the cell for the renderer control when printing to PDF.

GetValidators

Gets the validator collection, overwrite this method to allow customizing the returned result from the Validators property.

PaintCell

Gets a control and renders it in the cell based on the specified appearance, margin, and value.

Parse

When deriving a cell type based on this type, override this to change what gets parsed from the cell on the sheet and put in the data model.

RendererClientScriptUrl2

Gets the URL of the client script file for the renderer for the specified Spread component, if the renderer supports client-side scripting.

SetTabIndex

Sets TabIndex for the editor.

ToString

Gets the name of the cell type.

ValidateEditorValue

Validates the given value.

The Format method is used to get the string value to put in the clipboard text for the cell. The Parse method is used to convert that clipboard text back into a value when pasting. The methods are also generally used for converting the raw value to a string and back when editing, but that depends on the implementation of the cell type (most built-in cell types operate that way but a custom cell type does not need to). For example, the custom cell type might allow the user to edit the value as a URL string, and render it with an image tag, if the custom cell type displays an image. In that case, Format or Parse might not actually be needed since it would just copy or paste the raw URL value and also use that in the data model. You can override any of the following properties in the BaseCellType class:

EditorClientScriptUrl

Gets the URL of the client script file for the editor, if the editor supports client-side scripting.

RendererClientScriptUrl

Gets the URL of the client script file for the renderer, if the renderer supports client-side scripting.

Validators

Gets the validator collection.

The RenderClientScriptUrl2 method and the RenderClientScriptUrl and EditorClientScriptUrl properties do not take effect with Internet Explorer 10 or higher or with other browsers such as Firefox or Chrome. They are provided for the older style HTC model. This example creates a custom cell from a combo cell and overrides the GetEditorControl method to disable a specific item in the drop-down list. C#

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using FarPoint.Web.Spread;  

namespace SpreadASPNET8CSharp  
{  
    public partial class WebForm1 : System.Web.UI.Page  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            customCombo ComboCell = new customCombo();  
            ComboCell.Items = new String[] { "Jan", "Feb", "March", "April" };  
            ComboCell.BackColor = System.Drawing.Color.Aqua;  
            FpSpread1.Sheets[0].Cells[0, 0].CellType = ComboCell;  
        }  
    }  

    [Serializable()]  
    public class customCombo : FarPoint.Web.Spread.ComboBoxCellType  
    {  
        public override Control GetEditorControl(string id, TableCell parent, Appearance style, Inset margin, object value, bool upperLevel)  
        {  
            DropDownList ddl = (System.Web.UI.WebControls.DropDownList)base.GetEditorControl(id, parent, style, margin, value, upperLevel);  

            if (ddl != null)  
            {  
                foreach (System.Web.UI.WebControls.ListItem item in ddl.Items)  
                {  
                    if (item.Value == "Feb")  
                    {  
                        item.Attributes.Add("disabled", "disabled");  
                    }  
                }  
            }  
            return ddl;  
        }  

    }  
}  

VB

Imports FarPoint.Web.Spread  

Public Class WebForm1  
    Inherits System.Web.UI.Page  
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
        Dim ComboCell As New customCombo  
        ComboCell.Items = New String() {"Jan", "Feb", "March", "April"}  
        ComboCell.BackColor = Drawing.Color.Aqua  
        FpSpread1.Sheets(0).Cells(0, 0).CellType = ComboCell  
    End Sub  
End Class  

<Serializable()> Public Class customCombo  
    Inherits FarPoint.Web.Spread.ComboBoxCellType  

    Public Overrides Function GetEditorControl(id As String, parent As TableCell, style As Appearance, margin As Inset, value As Object, upperLevel As Boolean) As Control  
        Dim ddl As DropDownList = CType(MyBase.GetEditorControl(id, parent, style, margin, value, upperLevel), System.Web.UI.WebControls.DropDownList)  
        If IsNothing(ddl) Then Exit Function  
        For Each item As System.Web.UI.WebControls.ListItem In ddl.Items  
            If item.Value = "Feb" Then  
                item.Attributes.Add("disabled", "disabled")  
            End If  
        Next  
        Return ddl  
    End Function     
End Class  

You can download a larger example that demonstrates custom cells and renderers from this link: EditorRendererCS. The zip file does not contain the Spread ASP.NET assemblies.

MESCIUS inc.

comments powered by Disqus