列中的Datagridview掩码值
本文关键字:掩码 Datagridview | 更新日期: 2023-09-27 17:59:30
如何在windows窗体应用程序中"屏蔽"数据网格视图的值?例如,如何限制列datagridviewtextboxcolumn中的值,使其不大于给定的数字?(即该列中的单元格值<9.6)我在运行时以编程方式构建数据网格视图。
您可以在CellEndEdit事件处理程序上使用if()
如果可能的话,最简单的方法是在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
事件并验证输入。