列中的Datagridview掩码值

时间:2011-06-20 12:58:24

标签: c# datagridview masking

如何在Windows窗体应用程序中“屏蔽”datagridview的值?在示例中,如何限制列datagridviewtextbox列中的值,使其不大于给定数字? (即该列中的单元格值<9.6) 我在运行时以编程方式构建了datagridview。

2 个答案:

答案 0 :(得分:3)

您可以在CellEndEdit事件处理程序

上使用if()

答案 1 :(得分:2)

如果可能,最简单的方法是验证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事件并验证输入。

相关问题