为单元格指定数据类型
如何在代码中为单元格指定数据类型?只能在SetValue之前对数据类型进行约束吗? 回复 1楼swejet的帖子Spread 提供了验证机制-DataValidator:
gcSpreadSheet1.HighlightInvalidData = true;
var valid = GrapeCity.Windows.SpreadSheet.Data.DataValidator.CreateListValidator("5,10,15,20");
gcSpreadSheet1.Sheets.Cells.DataValidator = valid;
gcSpreadSheet1.Sheets.Cells.Text = "5, 10, 15, and 20 are valid numbers.";
var valid1 = GrapeCity.Windows.SpreadSheet.Data.DataValidator.CreateNumberValidator(GrapeCity.Windows.SpreadSheet.Data.ComparisonOperator.GreaterThan, "5", "20", true);
gcSpreadSheet1.Sheets.Cells.DataValidator = valid1;
gcSpreadSheet1.Sheets.Cells.Text = "A number greater than 5 is valid.";
var valid2 = GrapeCity.Windows.SpreadSheet.Data.DataValidator.CreateTextLengthValidator(GrapeCity.Windows.SpreadSheet.Data.ComparisonOperator.GreaterThan, "4", "20");
gcSpreadSheet1.Sheets.Cells.DataValidator = valid2;
gcSpreadSheet1.Sheets.Cells.Text = "Type more than four characters.";
并且可以通过 ValidationError 来接收错误输入字符。 那么用于公式计算的单元格内的值就是依赖于 Cell.SetValue之前这个值的类型了? 就是 setValue(int) 里面的值就作为int处理 可以计算出数值 setValue(string)就作为0计算? 回复 3楼swejet的帖子
是的,你理解的是正确的。
如果是通过 Setvalue 方法来设置值,可以在设置之前校验。
如果是用户在前台设置,可以通过 SheetView 的 CellChanged 事件来捕获:
public MainPage()
{
InitializeComponent();
double dvalue = 12345.6789;
gcSpreadSheet1.ActiveSheet.CellChanged += ActiveSheet_CellChanged;
gcSpreadSheet1.Sheets.Cells.Formatter = new GrapeCity.Windows.SpreadSheet.Data.GeneralFormatter(GrapeCity.Windows.SpreadSheet.Data.FormatMode.StandardNumericMode, "C");
gcSpreadSheet1.Sheets.Cells.Value = dvalue;
gcSpreadSheet1.Sheets.Cells.Formatter = new GrapeCity.Windows.SpreadSheet.Data.GeneralFormatter(GrapeCity.Windows.SpreadSheet.Data.FormatMode.StandardNumericMode, "C");
gcSpreadSheet1.Sheets.Cells.Value = dvalue;
gcSpreadSheet1.Sheets.Cells.Formatter = new GrapeCity.Windows.SpreadSheet.Data.GeneralFormatter(GrapeCity.Windows.SpreadSheet.Data.FormatMode.StandardNumericMode, "C");
gcSpreadSheet1.Sheets.Cells.Formula = "SUM(A1:B1)";
gcSpreadSheet1.Invalidate();
}
void ActiveSheet_CellChanged(object sender, GrapeCity.Windows.SpreadSheet.Data.CellChangedEventArgs e)
{
}
好的 谢谢 回复 5楼swejet的帖子
客气了,有问题欢迎继续交流。
页:
[1]