Jump to content

How to update the row number after removing rows from a table?


PBR
Go to solution Solved by Jose Leviaguirre,

Recommended Posts

Hello,

I have a table that includes multiple columns in which one of the columns is named "ID" corresponds to the number of records.
I  also have a delete button to remove any selected rows from the table. However, the row number under "ID" is getting messed up if I delete a row in between. So, I was looking to update the numbers for "ID" column as I delete the rows.

Here is my IronPython script but not sure exactly how to update that.

from Spotfire.Dxp.Data import IndexSet, DataValueCursor

dataTable = Document.Data.Tables["Events"]
markings = Document.ActiveMarkingSelectionReference.GetSelection(dataTable)

# Remove the selected rows
dataTable.RemoveRows(markings)

# Get reference to the 'ID' column
idColumn = dataTable.Columns["ID"]
idCursor = DataValueCursor.Create(idColumn)

rowCounter = 1

# Create an IndexSet to cover all remaining rows after deletion
remainingRows = IndexSet(dataTable.RowCount, True)

# Update the ID in the remaining rows
for rowIndex in range(dataTable.RowCount):
    if remainingRows.Contains(rowIndex):
        dataTable.SetValue(idCursor, rowIndex, rowCounter)
        rowCounter += 1

 but I am getting this error: 

TypeError: expected IndexSet, got int

I could update the numbers through a data function and introducing a new column but I am looking to do that through IronPython.

Thank you

Edited by PBR
Link to comment
Share on other sites

Thank you Gaia,

I was doing a similar method in data functions, but I want to avoid creating a new column and keep the same column. That would affect the rest of my jobs doing on the table.

Link to comment
Share on other sites

  • Solution

Hello @PBR I think that all you need is to remove the selection and add a calculated column using RowID()

dataTable = Document.Data.Tables["Events"]
markings = Document.ActiveMarkingSelectionReference.GetSelection(dataTable)
dataTable.RemoveRows(markings)

The ID column is simply a calculated column. It's custom expression is:

RowId() as [ID]

deleterows.gif.2552bc367f0198da232b821de030ec3a.gif

 

Edited by Jose Leviaguirre
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...