# Extracting whole row of data from selected row in table

**URL:** <https://forum.inductiveautomation.com/t/extracting-whole-row-of-data-from-selected-row-in-table/21962>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [January 18, 2019, 2:03am UTC](https://forum.inductiveautomation.com/t/extracting-whole-row-of-data-from-selected-row-in-table/21962 "2019-01-18T02:03:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mriederer](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@mriederer](https://forum.inductiveautomation.com/u/mriederer)\
**Post date:** [January 18, 2019, 2:03am UTC](https://forum.inductiveautomation.com/t/extracting-whole-row-of-data-from-selected-row-in-table/21962/1 "2019-01-18T02:03:24Z")

</div>

Is there an easy way to get the entire row of data from a table into a custom property? I feel like I’m missing something!

First, I tried binding a dataset type property as:  
{Root Container.Power Table.data}[{Root Container.Power Table.selectedRow}]

Secondly:  
I tried making a script to do this for me but I get the error:  
TypeError: toPyDataSet(): 1st arg can’t be coerced to com.inductiveautomation.ignition.common.Dataset

data = project.getData.getData()

headers = data.getColumnNames()  
a = []

row = 30

for col in range(len(headers)):  
colName = data.getColumnName(col)  
a.append([data.getValueAt(row, colName)])

#ds = system.dataset.toDataSet(headers, a)

ds = system.dataset.toPyDataSet(a)

Ugg, I really am missing something. Just want to be able to pass selected data from a table without creating a new SQL table…

---

<div class="post-metadata">

**Author:** ![c.bertulli](https://avatars.discourse-cdn.com/v4/letter/c/f19dbf/32.png) [@c.bertulli](https://forum.inductiveautomation.com/u/c.bertulli)\
**Post date:** [January 18, 2019, 7:55am UTC](https://forum.inductiveautomation.com/t/extracting-whole-row-of-data-from-selected-row-in-table/21962/2 "2019-01-18T07:55:44Z")

</div>

I don’t really like the idea of get every value and the create a new dataset. I’d prefer to eliminate every other rows so I can obtain a new dataset with only the selected row 😉  
Here it is the script:

```auto
table = event.source.parent.getComponent('Power Table')
selRow = table.selectedRow
PyTable = system.dataset.toPyDataSet(table.data)
IndicesToDelete = []

for x in range(0,len(PyTable)):
	if x != selRow:
		IndicesToDelete.append(x)

oneRow = system.dataset.deleteRows(PyTable,IndicesToDelete)

```

In oneRow you’ll have your new Dataset with only the selected row ✌

PS: The next time, put your code in “Preformatted Text (Ctrl + Shift + C )” so it can be more readable like the one I posted 😉

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [January 18, 2019, 8:52am UTC](https://forum.inductiveautomation.com/t/extracting-whole-row-of-data-from-selected-row-in-table/21962/3 "2019-01-18T08:52:05Z")

</div>

Both methods can be made a lot cleaner too (at least more pythonic) with list comprehensions and other transforms:

A way to calculate the indices

```auto
IndicesToDelete = [i for i in range(len(PyTable)) if x != selRow]

```

And a way to create the new array, by transforming it to a pyDataSet first:

```auto
pyDs = system.dataset.toPyDataSet(ds)
headers = system.dataset.getColumnHeaders(ds)
row = 30

newDs = system.dataset.toDataSet(headers, [pyDs[row]])

```

Although I’m not a big fan of these dataset methods. They lack the mutability and OO methods you expect in Python, which makes all Python code for it more awkward.

---

<div class="post-metadata">

**Author:** ![c.bertulli](https://avatars.discourse-cdn.com/v4/letter/c/f19dbf/32.png) [@c.bertulli](https://forum.inductiveautomation.com/u/c.bertulli)\
**Post date:** [January 18, 2019, 9:11am UTC](https://forum.inductiveautomation.com/t/extracting-whole-row-of-data-from-selected-row-in-table/21962/4 "2019-01-18T09:11:47Z")

</div>

> [@Sanderd17](#):
>
> Both methods can be made a lot cleaner too (at least more pythonic) with list comprehensions and other transforms:

Yeah I know, but since the code he posted was "basic" I followed his wave for a more "readable" code 😅

---

<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:** [January 18, 2019, 2:08pm UTC](https://forum.inductiveautomation.com/t/extracting-whole-row-of-data-from-selected-row-in-table/21962/5 "2019-01-18T14:08:52Z")

</div>

Try the [view()](https://www.automation-pros.com/simaids/doc/expression.view.html) expression function from the free [Simulation Aids](https://inductiveautomation.com/moduleshowcase/module/automation-professionals-llc-simulation-aids) Module. The expression would look something like this:

```auto
view("Select * Where _r == args[0] Limit 1",
  {Root Container.Power Table.data},
  {Root Container.Power Table.selectedRow})

```

---

<div class="post-metadata">

**Author:** ![mriederer](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@mriederer](https://forum.inductiveautomation.com/u/mriederer)\
**Post date:** [January 18, 2019, 6:37pm UTC](https://forum.inductiveautomation.com/t/extracting-whole-row-of-data-from-selected-row-in-table/21962/6 "2019-01-18T18:37:31Z")

</div>

Great, thanks for the tips. System.dataset.deleteRows worked like a champ, I guess I won’t try to figure out why the list-\>dataset conversion wasn’t working before.

```auto
def getRowData(data, selRow):
    IndicesToDelete = [i for i in range(len(data)) if i != selRow]
    data = system.dataset.deleteRows(data, IndicesToDelete)
    data = system.dataset.toPyDataSet(data)
    return data

```

I bound the new dataset to a custom property via the _onMousePress_ event handler because I couldn’t figure out how to pass dataset into a runScript command and bind it using the expression language:

```auto
data = self.data
self.rowData = project.getData.getRowData(data,rowIndex)

```

---

<div class="post-metadata">

**Author:** ![jpark](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@jpark](https://forum.inductiveautomation.com/u/jpark)\
**Post date:** [January 18, 2019, 10:47pm UTC](https://forum.inductiveautomation.com/t/extracting-whole-row-of-data-from-selected-row-in-table/21962/7 "2019-01-18T22:47:47Z")

</div>

```auto
if event.propertyName == "selectedRow":
	selectedRow = event.newValue
	data = event.source.data
	columns = system.dataset.getColumnHeaders(data)
	if selectedRow != -1:
		selRow = [list(system.dataset.toPyDataSet(data)[selectedRow])]
	else:
		selRow = []
	event.source.parent.getComponent('Power Table 2').data = system.dataset.toDataSet(columns, selRow)

```
