# Modifying PMIComboxBox DataSetComboBoxModel clears selected item

**URL:** <https://forum.inductiveautomation.com/t/modifying-pmicomboxbox-datasetcomboboxmodel-clears-selected-item/30525>\
**Category:** Ignition\
**Tags:** vision\
**Created:** [December 9, 2019, 2:29am UTC](https://forum.inductiveautomation.com/t/modifying-pmicomboxbox-datasetcomboboxmodel-clears-selected-item/30525 "2019-12-09T02:29:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [December 9, 2019, 2:29am UTC](https://forum.inductiveautomation.com/t/modifying-pmicomboxbox-datasetcomboboxmodel-clears-selected-item/30525/1 "2019-12-09T02:29:07Z")

</div>

I’m using my code in [this post](http://forum.inductiveautomation.com/t/dropdown-list-pmicombobox-disable-item-from-selection/23697/3?u=nminchin) to make changes to the drop down menu component’s `DataSetComboBoxModel` and `NewPMIComboBoxRenderer` classes, however it’s always had the issue where the component will show `<Select One>` when the `DataSetComboBoxModel` is replaced. I can work around this by getting the selected item prior to replacing the model, then after replacing it, writing it back to it, but I wonder how I can do this within the class itself?

The section of interest is this:

```auto
class NewPMIComboBoxModel(DataSetComboBoxModel):
	def __init__ (self, target):
		DataSetComboBoxModel. __init__ (self, target, target.data)
		self.target = target

```

As soon as I run the below, the drop down selection will be lost and `<Select One>` will display:

```auto
RC = system.gui.getParentWindow(event).rootContainer
PMIComboBox = RC.getComponent("Dropdown")
PMIComboBox.setModel(NewPMIComboBoxModel(PMIComboBox))

```

How can I maintain the selection?  
Cheers

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 9, 2019, 2:59am UTC](https://forum.inductiveautomation.com/t/modifying-pmicomboxbox-datasetcomboboxmodel-clears-selected-item/30525/2 "2019-12-09T02:59:15Z")

</div>

In your subclass of `DataSetComboBoxModel`, call `self.setSelectedItem()` within your ` __init__ ()` method, so that it is already “selected” when the model object replacement takes effect.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [December 9, 2019, 3:05am UTC](https://forum.inductiveautomation.com/t/modifying-pmicomboxbox-datasetcomboboxmodel-clears-selected-item/30525/3 "2019-12-09T03:05:59Z")

</div>

Hmm, I thought I’d tried this already but I have it working now. Actually I was calling `self.target.setSelectedItem()` instead.  
Thanks! 🙂

Strangely though, when I run `print self.getSelectedItem()`, it still returns `<Select One>`, but the component itself shows the actual selected item… `print target.getSelectedItem()` alternatively shows the actual selected item.

Running either `self.setSelectedItem(self.getSelectedItem())` or `self.setSelectedItem(target.getSelectedItem())` both ensure the item is selected.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 9, 2019, 3:07am UTC](https://forum.inductiveautomation.com/t/modifying-pmicomboxbox-datasetcomboboxmodel-clears-selected-item/30525/4 "2019-12-09T03:07:37Z")

</div>

> [@nminchin](#):
>
> but I have it working now.

Good timing. Time for me to crash. (:

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 9, 2019, 3:13am UTC](https://forum.inductiveautomation.com/t/modifying-pmicomboxbox-datasetcomboboxmodel-clears-selected-item/30525/5 "2019-12-09T03:13:07Z")

</div>

FWIW, I would use:

```auto
        try:
            self.selectedItem = target.model.selectedItem
        except:
            pass

```

I always use netbeans syntax for readability, and I tend to be paranoid about unexpected failures.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [December 9, 2019, 3:17am UTC](https://forum.inductiveautomation.com/t/modifying-pmicomboxbox-datasetcomboboxmodel-clears-selected-item/30525/6 "2019-12-09T03:17:01Z")

</div>

Noted and good point RE try/except. Thanks again!
