Murlex
Murlex

Reputation: 239

PropertyGrid clearing out a value

I have a property grid that has empty values to start with. The values are optional. If a user accidentally enters data in one of the properties, and clears it out, the grid doesn't allow them to clear it. For example if the property was of type uint32, it expects something in the range of uint32. How I can make the grid accept the value being empty?

Upvotes: 1

Views: 1203

Answers (1)

Hans
Hans

Reputation: 13030

You can declare your properties with nullable types (e.g. int?) to make the propertygrid accept empty values. Nullable types represent the range of the underlying data type plus the null value.

Here is a small example using a nullable int:

// Your class for which the property grid should display properties
public class YourClass
{    
  public int? MyIntValue        // Nullable int
  {
    get;set;     
  }

  public string MyStringValue
  {
    get;set;
  }
}

public partial class Form1 : Form
{
  private YourClass yourClass;

  // In your winform
  private void Form1_Load(object sender, EventArgs e)
  {
    yourClass = new YourClass();

    // Set selected object
    propertyGrid1.SelectedObject = yourClass;
  }
}

Upvotes: 2

Related Questions