自定义CellType显示任意文本内容

发布时间:2012/11/13 00:11 发布者:葡萄城产品团队

返回博客中心

在实际系统中,为了便于数据更容易被理解,我们需要将一些数据显示为用户一看就明白文本,比如说:发帖的叫楼主,第一个回帖的叫沙发,第二个回帖的叫板凳。该需求在Spread中可以通过自定义CellType非常方便的实现。

首先、实现一个自定义CellType,并定义一个Dictionary<string, string>类型的属性Items,该属性用于接收实际值与显示文本,然后重写PaintCell方法:

 
public class MyCellType : FarPoint.Win.Spread.CellType.GeneralCellType

    {



        public System.Collections.Generic.Dictionary<string, string> Items

        { 

            get; 

            set; 

        }



        public override void PaintCell(Graphics g, Rectangle r, FarPoint.Win.Spread.Appearance appearance, object value, bool isSelected, bool isLocked, float zoomFactor)

        {

            if (value != null)

            {

                if (Items.ContainsKey(value.ToString()))

                {

                    value = Items[value.ToString()];

                }

                else

                {

                    value = "什么都不是";

                }

            }

            base.PaintCell(g, r, appearance, value, isSelected, isLocked, zoomFactor);

        }

    }

复制代码

下面就可以使用自定义的CellType了:

  1.  
    private void Form1_Load(object sender, EventArgs e)
    
        {
    
            MyCellType ctp = new MyCellType();
    
    
    
            Dictionary<string, string> items = new Dictionary<string, string>();
    
            items.Add("1", "楼主");
    
            items.Add("2", "沙发");
    
            items.Add("3", "板凳");
    
            items.Add("4", "地板");
    
            ctp.Items = items;
    
    
    
            fpSpread1.ActiveSheet.Columns[0].CellType = ctp;
    
            fpSpread1.ActiveSheet.Cells[0, 0].Value = 3;
    
            fpSpread1.ActiveSheet.Cells[1, 0].Value = 2;
    
            fpSpread1.ActiveSheet.Cells[2, 0].Value = 5;
    
        }
    
    复制代码
    运行效果:


    VS2010 + Spread for .net 6.0
4065_CellType.zip (10.08 K, 下载次数:14)

关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网