# How to programmaticaly select dropdown selected index on window/form start

**URL:** <https://forum.inductiveautomation.com/t/how-to-programmaticaly-select-dropdown-selected-index-on-window-form-start/62828>\
**Category:** Ignition\
**Tags:** scripting, ignition81, vision\
**Created:** [August 9, 2022, 3:25pm UTC](https://forum.inductiveautomation.com/t/how-to-programmaticaly-select-dropdown-selected-index-on-window-form-start/62828 "2022-08-09T15:25:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff.ryan](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jeff.ryan/32/25543_2.png) [@jeff.ryan](https://forum.inductiveautomation.com/u/jeff.ryan)\
**Post date:** [August 9, 2022, 3:25pm UTC](https://forum.inductiveautomation.com/t/how-to-programmaticaly-select-dropdown-selected-index-on-window-form-start/62828/1 "2022-08-09T15:25:12Z")

</div>

Hello,  
I’m not quite getting there syntax-wise: I have a dropdown list component on a form whose data is bound to a SQL query that would populate it with several string items (‘names’) from a table.

When invoking this form at runtime, I need to select one of the items in this list/set based on a known column value (call it ‘OperatorName’) in a different table, and have that selected and shown.

In the scripting, Event Handlers on a property change for the dropdown list, I have been able select the first index with a **event.source.setSelectedIndex(0)**… But how do I iterate through the items in the list and select the one I need, finding the index for this ‘operator name’, rather than ‘0’, or the first? Thanks much!

---

<div class="post-metadata">

**Author:** ![bpreston](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@bpreston](https://forum.inductiveautomation.com/u/bpreston)\
**Post date:** [August 9, 2022, 3:50pm UTC](https://forum.inductiveautomation.com/t/how-to-programmaticaly-select-dropdown-selected-index-on-window-form-start/62828/2 "2022-08-09T15:50:06Z")

</div>

Without seeing your code I’m making assumptions but if your string is in the second column of your dataset that your searching and your index is in the first then something like this would return the first index that matches the string your searching for.

```auto
ds = dataset_you_want_to_search
p = [x[0] for x in ds if x[1] == 'operator name'][0]

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 9, 2022, 4:05pm UTC](https://forum.inductiveautomation.com/t/how-to-programmaticaly-select-dropdown-selected-index-on-window-form-start/62828/3 "2022-08-09T16:05:19Z")

</div>

Note that @bpreston’s code requires your data is already in the form of a pydataset. Also, a more efficient rewrite would be something like this:

```python
ds = dataset_you_want_to_search
p = next(index for index, operator in ds if operator == 'operator name', None)

```

Using the `next` function makes iteration ‘lazy’, meaning that as soon as _any_ match is found, a value is returned; using a square bracket list comprehension requires that the full list be traversed before any value can be returned.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [August 9, 2022, 4:42pm UTC](https://forum.inductiveautomation.com/t/how-to-programmaticaly-select-dropdown-selected-index-on-window-form-start/62828/4 "2022-08-09T16:42:25Z")

</div>

A nice little trick, unpacking a pydataset like that. Except that if I try it, I get an exception for too many values to unpack?

---

<div class="post-metadata">

**Author:** ![jeff.ryan](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jeff.ryan/32/25543_2.png) [@jeff.ryan](https://forum.inductiveautomation.com/u/jeff.ryan)\
**Post date:** [August 9, 2022, 4:59pm UTC](https://forum.inductiveautomation.com/t/how-to-programmaticaly-select-dropdown-selected-index-on-window-form-start/62828/5 "2022-08-09T16:59:53Z")

</div>

Thanks much, but I’m not showing 2 columns in this particular dataset of the ‘Data’ property of the dropdown (UserName here is coming from the SQL column it is bound to). I’m getting this error, of course there isn’t an index column. But. Event.sourse.setSelectedIndex(0) with a ‘0’ still works…

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/5/b5b2451051bf2de6a6f700a70975d31ebdb1bc82.png)

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/a/8a95ecb60d5a2e85dd30464e9f18a7b2f250334b.png)

![image001.png](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/f/4f612f937ab1b67c0572818270a259a3bd125e95.png)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 9, 2022, 5:10pm UTC](https://forum.inductiveautomation.com/t/how-to-programmaticaly-select-dropdown-selected-index-on-window-form-start/62828/6 "2022-08-09T17:10:15Z")

</div>

> [@lrose](#):
>
> Except that if I try it, I get an exception for too many values to unpack?

Unpacking like this would require you have exactly two columns in your pydataset. Maybe too fragile to be generally useful, but does make a limited approach like this more readable.

> [@jeff.ryan](#):
>
> Thanks much, but I’m not showing 2 columns in this particular dataset of the ‘Data’ property of the dropdown (UserName here is coming from the SQL column it is bound to). I’m getting this error, of course there isn’t an index column. But. Event.sourse.setSelectedIndex(0) with a ‘0’ still works…

I'm somewhat unclear on exactly what the flow of the logic is supposed to be here.  
Right now, every time the `data` property is updated (by a query?), you're running this additional query and trying to match up names.  
Some questions:  
What if there are multiple matches?  
Could this be a `JOIN` operation at the DB level?

The specific error in your screenshot is just syntax; it wants you to change that line to something like this:  
`p = next((index for index, username in ds if username == row["Operator1"]), 0)`

But that will then fail with 'not enough values to unpack' because your dataset only has a single column, so try this:  
`p = next((index for index, username in enumerate(ds) if username == row["Operator1"]), 0)`

The `enumerate` builtin will "add" an index column to the iteration loop (effectively).

---

<div class="post-metadata">

**Author:** ![jeff.ryan](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jeff.ryan/32/25543_2.png) [@jeff.ryan](https://forum.inductiveautomation.com/u/jeff.ryan)\
**Post date:** [August 9, 2022, 6:37pm UTC](https://forum.inductiveautomation.com/t/how-to-programmaticaly-select-dropdown-selected-index-on-window-form-start/62828/7 "2022-08-09T18:37:27Z")

</div>

OK, I got it to work, thanks @paul-griffith .

The following gave me an error: ‘Object is not iterable’  
`p = next((index for index, username in enumerate(ds) if username == row["Operator1"]), 0)`

I tried fighting through it, looked that error up in a search, stumbled across the following example, and got it to work:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/7/6727744e84eb101bc2919fb0bd2d2ae81617cb35.png)

(I’m using ‘supervisor’ rather than operator in this example. I need to handle both). But this got me going down the right path.

“What if there are multiple matches”: There won’t be , that ‘name’ is a primary key in the table  
“Could this be a JOIN”: It’s not, as to the binding on the data property. It is selecting just one column

As to the flow of logic, I had originally got this code / logic handling sample for this some time ago. This for setting the selected index on a dropdown list, when just setting it to the first index (‘0’) rather than saying on a form load. This seems to work. Is there a better way? If the data were to change by a user selecting a different one from the list and ‘saving’ (I update it in the DB on click of ‘Save Values’), then I would want to display that new one. Thanks again!
