# Set a row in a table to selected based on a value in a row

**URL:** <https://forum.inductiveautomation.com/t/set-a-row-in-a-table-to-selected-based-on-a-value-in-a-row/31645>\
**Category:** Ignition\
**Tags:** ignition79, scripting\
**Created:** [January 27, 2020, 4:59pm UTC](https://forum.inductiveautomation.com/t/set-a-row-in-a-table-to-selected-based-on-a-value-in-a-row/31645 "2020-01-27T16:59:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lbuckley](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@lbuckley](https://forum.inductiveautomation.com/u/lbuckley)\
**Post date:** [January 27, 2020, 4:59pm UTC](https://forum.inductiveautomation.com/t/set-a-row-in-a-table-to-selected-based-on-a-value-in-a-row/31645/1 "2020-01-27T16:59:13Z")

</div>

I am trying to set a row in a table to selected when a column in the row is equal to 2. Does anyone have an idea of how I can achieve this?

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [January 27, 2020, 7:18pm UTC](https://forum.inductiveautomation.com/t/set-a-row-in-a-table-to-selected-based-on-a-value-in-a-row/31645/2 "2020-01-27T19:18:49Z")

</div>

This worked for me when executed from a button.

```python
dataIn = event.source.parent.getComponent('Table').data
# Get column values
colData = list(dataIn.getColumnAsList(dataIn.getColumnIndex('ColName')))

# Find first occurence of the value 2
rowIndex = colData.index(2)

# Set the row to the found index
event.source.parent.getComponent('Table').selectedRow = rowIndex

```

---

<div class="post-metadata">

**Author:** ![lbuckley](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@lbuckley](https://forum.inductiveautomation.com/u/lbuckley)\
**Post date:** [January 28, 2020, 8:46am UTC](https://forum.inductiveautomation.com/t/set-a-row-in-a-table-to-selected-based-on-a-value-in-a-row/31645/3 "2020-01-28T08:46:52Z")

</div>

Thanks Jordan, that worked perfectly.

---

<div class="post-metadata">

**Author:** ![lbuckley](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@lbuckley](https://forum.inductiveautomation.com/u/lbuckley)\
**Post date:** [January 28, 2020, 10:00am UTC](https://forum.inductiveautomation.com/t/set-a-row-in-a-table-to-selected-based-on-a-value-in-a-row/31645/4 "2020-01-28T10:00:10Z")

</div>

My table is on my main window. Do you know how I could run this script on a button on a popup window.

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [January 29, 2020, 9:53pm UTC](https://forum.inductiveautomation.com/t/set-a-row-in-a-table-to-selected-based-on-a-value-in-a-row/31645/5 "2020-01-29T21:53:17Z")

</div>

If I understand you correctly, the button to execute the script is in the popup but the table is in the main window?

if that’s the case, this should work if you execute it from the popup button. For simplicity I just modified @JordanCClark 's code.

```auto
# Pull reference to Main Window
mainWindow = system.gui.getWindow("MainWindowPathHere") 
# Reference the main table that is located on the main window
mainTable = mainWindow.rootContainer.getComponent("Table_Name_Here")  
#actual path may vary, if the main table in embedded in a group or multiple groups, you will have to add .getComponent("groupname") for each layer

dataIn = mainTable.data
#Get column values
colData = list(dataIn.getColumnAsList(dataIn.getColumnIndex("ColName")))

# Find first occurence of the value 2
rowIndex = colData.index(2)

# Set the row to the found index
mainTable.selectedRow = rowIndex

```

Also note, if for whatever reason the main window is closed and this script executes, you will get an error from the ‘system.gui.getWindow()’ function.

---

<div class="post-metadata">

**Author:** ![lbuckley](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@lbuckley](https://forum.inductiveautomation.com/u/lbuckley)\
**Post date:** [January 30, 2020, 9:54am UTC](https://forum.inductiveautomation.com/t/set-a-row-in-a-table-to-selected-based-on-a-value-in-a-row/31645/6 "2020-01-30T09:54:48Z")

</div>

Thanks Ryan, this works very well.
