This is a migrated thread and some comments may be shown as answers.

PropertyGridSpinEditor and null value

4 Answers 215 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 1
Joe asked on 03 Oct 2015, 12:29 AM

Hi, 

I'm currently populating the property grid using a RadPropertyStore.  One of my object's properties is a nullable Int32 field (Int32?) and I need to allow the user to empty out the PropertyGridSpinEditor cell.  Unfortunately, when the data cell is cleared out, it automatically gets set to a value of 0 instead of empty/null.

How can I allow the user to clear out the data and have null be considered a valid value?

Thanks,

Joe

PS.  See attached screenshot for current behavior.

4 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 05 Oct 2015, 02:08 PM
Hello Joe,

Thank you for writing.

PropertyGridSpinEditor expects a valid numeric value. When its value is cleared, 0 is set. In order to deal with this undesired behavior, you can use the ItemFormatting event and hide the text according to your criteria by setting the PropertyGridItemElement.ValueElement.DrawText property to false. Thus, 0 value will not be displayed:
private void radPropertyGrid1_ItemFormatting(object sender, PropertyGridItemFormattingEventArgs e)
{
    PropertyGridItemElement el = e.VisualElement as PropertyGridItemElement;
    if (el != null)
    {
        if (e.Item.Label == "Integer" && el.ValueElement.Text=="0")
        {
            el.ValueElement.DrawText = false;
        }
        else
        {
            el.ValueElement.DrawText = true;
        }
    }
}

An alternative solution is to implement a custom editor which will cover your requirement. Please refer to Using custom editor help article.

I hope this information helps. Should you have further questions I would be glad to help.
 
Regards,
Dess
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Joe
Top achievements
Rank 1
answered on 05 Oct 2015, 04:25 PM

Thanks for the reply, Dess.

Unfortunately, just hiding the text won't be enough as I need to distinguish between the user typing in 0 and the user clearing the value (i.e. null).

I guess I'll have to look into a custom editor.

Regards,

Joe

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 08 Oct 2015, 07:23 AM
Hello Joe,

Thank you for writing back.

I have prepared a sample code snippet demonstrating how you can detect whether the value is cleared or the it is set to 0:
public Form1()
{
    InitializeComponent();
 
    PropertyStoreItem intItem = new PropertyStoreItem(typeof(Int32?), "Integer", 2);
    PropertyStoreItem floatItem = new PropertyStoreItem(typeof(float), "Float", 1f, "Property storing a floating point value.");
    PropertyStoreItem stringItem = new PropertyStoreItem(typeof(string), "String", "telerik", "Property storing a string value", "Telerik");
   
    RadPropertyStore store = new RadPropertyStore();
    store.Add(intItem);
    store.Add(floatItem);
    store.Add(stringItem);
    
    this.radPropertyGrid1.SelectedObject = store; 
    this.radPropertyGrid1.EditorInitialized += radPropertyGrid1_EditorInitialized;
}
 
private void radPropertyGrid1_EditorInitialized(object sender, PropertyGridItemEditorInitializedEventArgs e)
{
    PropertyGridSpinEditor spinEditor = e.Editor as PropertyGridSpinEditor;
    if (spinEditor != null)
    {
        BaseSpinEditorElement el = spinEditor.EditorElement as BaseSpinEditorElement;
        el.ValueChanged -= el_ValueChanged;
        el.ValueChanged += el_ValueChanged;
        el.KeyUp += el_KeyUp;
    }
}
 
private void el_KeyUp(object sender, KeyEventArgs e)
{
    BaseSpinEditorElement el = sender as BaseSpinEditorElement;
    isCleared = false;
    if (el != null && el.Text == string.Empty)
    {
        isCleared = true;
    }
    else
    {
        isCleared = false;
    }
}
 
bool isCleared = false;
 
private void el_ValueChanged(object sender, EventArgs e)
{
    BaseSpinEditorElement el = sender as BaseSpinEditorElement;
    isCleared = false;
    if (el != null && el.Text == string.Empty)
    {
        isCleared = true;
    }
}
 
private void radPropertyGrid1_ItemFormatting(object sender, PropertyGridItemFormattingEventArgs e)
{
    PropertyGridItemElement el = e.VisualElement as PropertyGridItemElement;
    if (el != null)
    {
        if (e.Item.Label == "Integer" && el.ValueElement.Text == "0" && isCleared)
        {
            el.ValueElement.DrawText = false;
        }
        else
        {
            el.ValueElement.DrawText = true;
        }
    }
}

Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify it in a way which suits your requirement best. 

I hope this information helps. If you have any additional questions, please let me know.

Regards,
Dess
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Joe
Top achievements
Rank 1
answered on 08 Oct 2015, 03:19 PM

Thank you very much for the example, Dess - it's really appreciated.

I'm actually doing something very similar to suit it to my needs.

Regards,

Joe

Tags
PropertyGrid
Asked by
Joe
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Joe
Top achievements
Rank 1
Joe
Top achievements
Rank 1
Share this question
or