【发布时间】:2011-09-18 16:25:14
【问题描述】:
如何在 Windows 窗体应用程序中“屏蔽”datagridview 的值?例如,如何限制 datagridviewtextboxcolumn 列中的值,使其不大于给定数字? (即该列中的单元格值
【问题讨论】:
标签: c# datagridview masking
如何在 Windows 窗体应用程序中“屏蔽”datagridview 的值?例如,如何限制 datagridviewtextboxcolumn 列中的值,使其不大于给定数字? (即该列中的单元格值
【问题讨论】:
标签: c# datagridview masking
如果可能,最简单的方法是验证 entity 级别的值。
例如,假设我们有以下简化的Foo 实体;
public class Foo
{
private readonly int id;
private int type;
private string name;
public Foo(int id, int type, string name)
{
this.id = id;
this.type = type;
this.name = name;
}
public int Id { get { return this.id; } }
public int Type
{
get
{
return this.type;
}
set
{
if (this.type != value)
{
if (value >= 0 && value <= 5) //Validation rule
{
this.type = value;
}
}
}
}
public string Name
{
get
{
return this.name;
}
set
{
if (this.name != value)
{
this.name = value;
}
}
}
}
现在我们可以绑定到我们的DataGridView 和List<Foo> foos,我们将有效地屏蔽"Type" DataGridViewColumn 中的任何输入。
如果这不是有效路径,则只需处理 CellEndEdit 事件并验证输入。
【讨论】:
Type 字段DataGridViewCell 中输入8,当他提交值(按回车键)时,它将立即切换回原始值作为修改无法在底层entity中设置。
您可以只在 CellEndEdit 事件处理程序上使用 if()
【讨论】: