1

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

4

2 回答 2

3

您可以在 CellEndEdit 事件处理程序上使用 if()

于 2011-06-20T13:10:28.660 回答
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的 aList<Foo> foos并且我们将有效地屏蔽"Type" DataGridViewColumn.

如果这不是有效路径,则只需处理CellEndEdit事件并验证输入。

于 2011-06-20T13:37:59.147 回答