【Spread】通过 SubEditor 实现多种数据输入方式

发布时间:2013/04/18 00:04 发布者:jian

返回博客中心

在 Spreada .NET 中CellType是非常重要的一个功能,通过CellType可以实现数据的显示和数据编辑操作,今天主要讲解通过 Editor 属性实现用户指定的数据输入方式。

首先,我们添加一个用户输入数据的窗体 PopupEditorControl,该窗体需要实现 FarPoint.Win.Spread.CellType.ISubEditor 接口,代码如下:

    public partial class PopupEditorControl : Form, FarPoint.Win.Spread.CellType.ISubEditor
    {
        public event EventHandler CloseUp;
        public event EventHandler ValueChanged;

        public PopupEditorControl()
        {
            InitializeComponent();
        }


        public Point GetLocation(Rectangle rect)
        {
            // SubEditor的起始位置
            return new Point(0);
        }

        public Size GetPreferredSize()
        {
            // SubEditor的大小
            return this.Size;
        }

        public Control GetSubEditorControl()
        {
            return this;
        }

        public object GetValue()
        {
            return "10";
        }

        public void SetValue(object value)
        {

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            // 返回选中的数据
            ValueChanged(this, EventArgs.Empty);
            CloseUp(this, EventArgs.Empty);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            // 取消本次操作
            CloseUp(this, EventArgs.Empty);
        }
    }

接下来,我们添加一个CPopupCellType单元格类型,代码如下:

    class CPopupCellType : FarPoint.Win.Spread.CellType.TextCellType
    {        
        public CPopupCellType()
        {
            this.SubEditor = new PopupEditorControl();
        }
    }

然后,在主窗体中给Spread设置该单元格类型,代码如下:

    private void Form1_Load(object sender, EventArgs e)
    {

        fpSpread1.ActiveSheet.Cells[7, 1].Value = "通过自定义的CPopupCellType单元格类型,可以给用户提供更多的数据输入方式:";
        fpSpread1.ActiveSheet.Cells[7, 1].Font = new System.Drawing.Font("微软雅黑", 20f, FontStyle.Bold);
        fpSpread1.ActiveSheet.Rows[7].Height = 50;

        // 设置单元个类型
        fpSpread1.ActiveSheet.Cells[8, 1, 10, 3].CellType = new CPopupCellType();
    }

运行截图:

IEditor

源码下载:

VS2008 + Spread .NET 6

 

GridSamples.zip (20.20 kb)


关于葡萄城

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

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