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
  •  | 
  •  

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)

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top