在 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(); }
运行截图:
源码下载:
VS2008 + Spread .NET 6