Menu

Cannot set null on EditableObject column

2005-12-14
2013-03-07
  • Massimo Iacolare

    Hi,
    i'm not be able to set a null value on an ObjectViewRow. Going around the code the problem seem to be in ObjectViewRow.cs inside BeginEdit(), EndEdit() and GetColumnValue().
    BeginEdit() initializes newValues collection using 'null' with 'NotInitialized' meanings, thereby EndEdit() and GetColumnValue() compares values with 'null' to know if newValues contains valid values.

    I setup a simple Test to verify this behavior:

    [Test]
    public void SetNullValue()
    {
        ObjectView ov = new ObjectView(typeof(Customer));
        ov.Columns.Add(new ObjectViewColumn("Name", "Name"));

        Customer customer = new Customer(1, "Test Customer");
        IList customers = new ArrayList(1);
        customers.Add(customer);
        ov.DataSource = customers;
        ov.Rows[0].BeginEdit();
        ov.Rows[0]["Name"] = null;
        Assert.AreEqual("Test Customer", customer.Name);
        ov.Rows[0].EndEdit();
        Assert.AreEqual(null, customer.Name);
    }

    I attach a modified ObjectViewRow.cs that pass the test with comments on modified lines.

    Ciao
    Massimo

    PS:
    I cannot attach the file so i post here, sorry for the length

    internal object GetColumnValue(ObjectViewColumn column)
    {
        //Original
        //if (this.isEdit && this.newValues[column] != null)

        //Modified
        if (this.isEdit && !NotInitialized.Instance.Equals(newValues[column]))
        {
            return this.newValues[column];
        }
        ...
    }

    public void BeginEdit()
    {
        if (! this.isEdit)
        {
            foreach (ObjectViewColumn column in this.objectView.Columns)
            {
                //Original
                //this.newValues.Add(column, null);
               
                //Modified
                this.newValues.Add(column, NotInitialized.Instance);
            }
            this.isEdit = true;
        }
    }

    public void EndEdit()
    {
        if (this.isEdit)
        {
            // Copy temporary values into the real object.
            bool hasChanges = false;
            foreach (DictionaryEntry entry in this.newValues)
            {
                //Original
                //if (entry.Value != null)
               
                //Modified
                if (!NotInitialized.Instance.Equals(entry.Value))
                {
                    ((ObjectViewColumn)entry.Key).SetValue(this.innerObject, entry.Value);
                    hasChanges = true;
                }
            }
            ...
        }
    }

    //Dummy singleton
    public class NotInitialized
    ...

     
    • Martijn Boland

      Martijn Boland - 2005-12-15

      Thanks a lot! Could you perhaps create a Jira issue for this at http://objectviews.org/jira?

       
    • Massimo Iacolare

      Done!

      ObjectView make life a lot easier. Really good job!

       

Log in to post a comment.

MongoDB Logo MongoDB