在某些情况下我们会将Spread中某一列的单元格类型设置为 超链接(HyperLinkCellType ),并指定相应的 URL 地址(如: http://gcdn.grapecity.com ),然后会给 Spread 绑定数据源。

此时,我们会发现 HyperLinkCellType  所在的列,每个单元格的超链接地址都是指向同一个链接地址,有时候我们希望每个单元格的 URL 是不一样的,比如每个单元格 URL 中会带一个参数(如:http://gcdn.grapecity.com?id=100),类似这样的需求我们可以通过自定义的 HyperLinkCellType  类型来实现我们的需求,代码如下:

 

  • C#
 
[Serializable]
    public class MyHyperLinkCellType : FarPoint.Web.Spread.HyperLinkCellType
    {
        public override Control PaintCell(string id, TableCell parent, FarPoint.Web.Spread.Appearance style, FarPoint.Web.Spread.Inset margin, object value, bool upperLevel)
        {
            if (value != null)
            {
                HyperLink hl = base.PaintCell(id, parent, style, margin, value, upperLevel) as HyperLink;
                hl.Target = Target;
                hl.NavigateUrl = NavigateUrl + "?id=" + value.ToString();
                
                return hl;

            }
            else {
                return base.PaintCell(id, parent, style, margin, value, upperLevel);
            }
        }
    }
复制代码

 

  • VB.NET
 
<Serializable> _
Public Class MyHyperLinkCellType
    Inherits FarPoint.Web.Spread.HyperLinkCellType
    Public Overrides Function PaintCell(id As String, parent As TableCell, style As FarPoint.Web.Spread.Appearance, margin As FarPoint.Web.Spread.Inset, value As Object, upperLevel As Boolean) As Control
        If value IsNot Nothing Then
            Dim hl As HyperLink = TryCast(MyBase.PaintCell(id, parent, style, margin, value, upperLevel), HyperLink)
            hl.Target = Target
            hl.NavigateUrl = NavigateUrl & "?id=" & value.ToString()


            Return hl
        Else
            Return MyBase.PaintCell(id, parent, style, margin, value, upperLevel)
        End If
    End Function
End Class
复制代码

使用自定义的 MyHyperLinkCellType  类型:

 
protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            return;
        }

        MyHyperLinkCellType hlct = new MyHyperLinkCellType();
        hlct.NavigateUrl = "http://gcdn.grapecity.com/index.aspx";
        hlct.Target = "blank";

        hlct.ShowEllipsis = true;
        FpSpread1.ActiveSheetView.Columns[1].CellType = hlct;

        DataTable dt = new DataTable();

        dt.Columns.Add("Col1");
        dt.Columns.Add("Col2");
        dt.Columns.Add("Col3");

        dt.Rows.Add(1, 1, 3);
        dt.Rows.Add(1, 2, 3);
        dt.Rows.Add(1, 3, 3);
        dt.Rows.Add(1, 4, 3);
        dt.Rows.Add(1, 5, 3);

        FpSpread1.ActiveSheetView.DataAutoCellTypes = false;
        FpSpread1.ActiveSheetView.DataSource = dt;
            
    }
复制代码