在实际系统中,为了便于数据更容易被理解,我们需要将一些数据显示为用户一看就明白文本,比如说:发帖的叫楼主,第一个回帖的叫沙发,第二个回帖的叫板凳。该需求在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了:
-
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