Databound ComboBox reverts to original value

Posts   
 
    
Waveslam
User
Posts: 18
Joined: 20-Nov-2006
# Posted on: 26-Nov-2006 21:54:06   

Hi again I am still working on the issues I described in my thread http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=8169.

Basically, I want a combobox that is loaded with an Entity COllection, PLUS an additional item "<All>". I wont post all the code unless anybody wants it but basically to do this: 1. I created a custom class to represent each item that will appear in the COmboBox:

        private class ObjectOptionalComboBoxItem
        {
            private IbObjectEntity ibObjectEntity;
            public IbObjectEntity IbObjectEntity
            {
                get { return ibObjectEntity; }
                set { ibObjectEntity = value; }
            }

            // This is the ValueMember of the combo 
            private int returnValue;
            public int ReturnValue
            {
                get { return returnValue; }
                set { returnValue = value; }
            }

            // This is the DisplayMember of the combo 
            private String displayText;
            public String DisplayText
            {
                get { return displayText;}
                set { displayText = value;}
            }
        };

2.At runtime I create a List<> of these, and the first item in the list is the "<All>" entry

            ObjectOptionalComboBoxItem item;
            List<ObjectOptionalComboBoxItem> itemList = new List<ObjectOptionalComboBoxItem>();
            // Add the "<All>" entry first
            item = new ObjectOptionalComboBoxItem();
            item.IbObjectEntity = null;
            item.ReturnValue = -1;
            item.DisplayText = kAllEntryText;
            itemList.Add(item);

  1. Remainder of the list is populated by fetching an Entity List and iterating through it, adding the objects to my custom List<>
    
            IbObjectCollection ibObjectList;
            ObjectDataCacheManager cacheManager = ObjectDataCacheManager.GetInstance;
            ibObjectList = cacheManager.FindChildrenOf(ParentObID);
            foreach (IbObjectEntity ibObjectEntity in ibObjectList)
            {
                item = new ObjectOptionalComboBoxItem();
                item.IbObjectEntity = ibObjectEntity;
                item.DisplayText = ibObjectEntity.ObName;
                item.ReturnValue = ibObjectEntity.ObId;
                itemList.Add(item);
            }

  1. I set the list's DataSource to my custom list, and the DisplayMember and ValueMember properties to the relevant properties of my custom list item class:
            this.DataSource = itemList;
            this.ValueMember = "ReturnValue";
            this.DisplayMember = "DisplayText";

  1. Finally, I bind this combobox object to a member in an Entity row I have fetched from the DB ie. to a "database column".
            this.ocbComponent.DataBindings.Add(new Binding("SelectedValue", IbBugRow, "BuComponent")); 

What I am now seeing is that the combobox appears to bind OK, as the entry that is initially selected in the list when it appears matches the DB value.

HOWEVER, I cannot seem to change the value: I can pick another entry from the list but as soon as I navigate out of the combobox, the selected item reverts to the original value.

Has anybody seen this before and/or got any idea where I am going wrong?? The specific DB column (SQL Server 2000) is a nullable int, in case it helps, although I have been testing with non-null values and it still doesn't work. I am using LLGLGenPro 2.0.0.0 DEMO version, SelfServicing, .NET 2.0, Windows Forms.

Thanks heaps in advance Cheers Brett

Waveslam
User
Posts: 18
Joined: 20-Nov-2006
# Posted on: 27-Nov-2006 02:04:48   

Right, turns out that this problem only occurs if you databind the control to a nullable type (which my particular DB column was).

I changed the data binding to just use a private member variable/property of the form itself ie.

        private int m_component = -1;
        public int ComponentID
        {
            get { return m_component; }
            set { m_component = value; }
        }

    . . . 
            this.ocbComponent.DataBindings.Add(new Binding("SelectedValue", this, "ComponentID")); 


And it works fine. However, make the variable/property nullable like this:

        private int? m_component = -1;
        public int? ComponentID
        {
            get { return m_component; }
            set { m_component = value; }
        }

    . . . 
            this.ocbComponent.DataBindings.Add(new Binding("SelectedValue", this, "ComponentID")); 


And it doesn’t work, giving the same problem I was seeing i.e. combobox reverts to the initial value when you navigate away from it.

I couldn't find any specific reference to databinding using nullable types not being supported, so was stumped again. However, through sheer luck, I have miraculously been able to solve the problem by replacing:

      this.ocbComponent.DataBindings.Add(new Binding("SelectedValue", IbBugRow, "BuComponent")); 

with

      this.ocbComponent.DataBindings.Add(new Binding("SelectedValue", IbBugRow, "BuComponent", true));

.NET Databidning now officially owes me 4 days of my life...

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 27-Nov-2006 08:07:48   

Glad you find it out, and thanks for sharing the information.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 27-Nov-2006 09:53:19   

.NET Databidning now officially owes me 4 days of my life...

Consider yourself lucky wink

Yeah, databinding and generics are two things that won't go together that well... Which is odd, as it's all .NET and generics and also nullable types were in the framework for quite some time.

Frans Bouma | Lead developer LLBLGen Pro