Is there any way to mark string field as "No nulls or empty strings allowed" for PropertyGrid control?

StackOverflow https://stackoverflow.com/questions/20910327

  •  24-09-2022
  •  | 
  •  

Question

Currently property grid allows me to change string value of field to empty string, but I use this field as ID. It's should not be empty.

Also, maybe there is some way to forbid or cancel change of field's value? (In case the ID entered is already exist)

Was it helpful?

Solution

If you use a property instead, you can add logic to the setter to not allow null or empty strings. You can also add the logic to see if it exists.:

        public string TestString
    {
        get { return TestString; }

        set 
        {
            if(!string.IsNullOrEmpty(value))
            {
                // TODO: Add your logic to check if exists somwhere
                TestString = value;
            }
            else
            {
                throw new ArgumentOutOfRangeException("value", " etc... ");
            }
        } 

    }

Throwing the exception may cause a heated debate, but its your choice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top