Datagridview mask values ​​in a column

How can I "mask" datagridview values ​​in a windows forms application? In the example, how can I limit the value in the datagridviewtextboxcolumn column so that it does not exceed the given number? (i.e. the cell value in this column is <9.6) I create my datagridview programmatically at runtime.

+1
source share
2 answers

You can simply use if () for the CellEndEdit event handler

+3
source

The easiest way to do this, if possible, is to check the value at entity level.

For example, let's say we have the following simplified 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; } } } } 

Now we can bind to our DataGridView a List<Foo> foos , and we will effectively mask any input in the "Type" DataGridViewColumn .

If this is not a valid path, simply handle the CellEndEdit event and confirm the entry.

+2
source

All Articles