第15章-自定义单元格类型
第十五章:自定义单元格类型
15.1 自定义单元格类型基础
ReoGrid允许创建自定义的单元格类型,实现特殊的显示和交互。
using unvell.ReoGrid.CellTypes;
public class CustomCellType : CellBody
{
public override void OnPaint(CellDrawingContext dc)
{
// 自定义绘制逻辑
base.OnPaint(dc);
}
public override bool OnMouseDown(CellMouseEventArgs e)
{
// 自定义鼠标事件处理
return base.OnMouseDown(e);
}
}
15.2 内置单元格类型
public class BuiltInCellTypes
{
public void UseBuiltInTypes(Worksheet sheet)
{
// 按钮单元格
var buttonCell = new ButtonCell("Click Me");
buttonCell.Click += (s, e) =>
{
MessageBox.Show("按钮被点击");
};
sheet.SetCellBody(0, 0, buttonCell);
// 复选框单元格
var checkBoxCell = new CheckBoxCell();
checkBoxCell.CheckChanged += (s, e) =>
{
Console.WriteLine($"复选框状态: {checkBoxCell.IsChecked}");
};
sheet.SetCellBody(1, 0, checkBoxCell);
// 下拉列表单元格
var comboBoxCell = new DropdownListCell("选项1", "选项2", "选项3");
sheet.SetCellBody(2, 0, comboBoxCell);
}
}
15.3 图像单元格
public class ImageCellType
{
public void UseImageCell(Worksheet sheet, string imagePath)
{
var imageCell = new ImageCell(Image.FromFile(imagePath));
sheet.SetCellBody(0, 0, imageCell);
}
}
15.4 自定义进度条单元格
public class ProgressBarCell : CellBody
{
private float progress = 0;
public float Progress
{
get => progress;
set
{
progress = Math.Max(0, Math.Min(1, value));
this.Cell?.Worksheet?.RequestInvalidate();
}
}
public override void OnPaint(CellDrawingContext dc)
{
var bounds = this.Cell.Bounds;
// 绘制背景
dc.Graphics.FillRectangle(new SolidBrush(Color.LightGray), bounds);
// 绘制进度
var progressWidth = bounds.Width * progress;
dc.Graphics.FillRectangle(
new SolidBrush(Color.Green),
bounds.X, bounds.Y, progressWidth, bounds.Height
);
// 绘制文本
var text = $"{(progress * 100):F0}%";
dc.Graphics.DrawString(text, this.Cell.Style.Font, Brushes.Black, bounds);
}
}
// 使用示例
public class UseProgressBar
{
public void Example(Worksheet sheet)
{
var progressCell = new ProgressBarCell { Progress = 0.75f };
sheet.SetCellBody(0, 0, progressCell);
}
}
15.5 本章小结
本章介绍了自定义单元格类型的创建和使用。
📚 下一章预告
第十六章将学习脚本与宏功能。

浙公网安备 33010602011771号