Live Chat

We'll need to share your messages (and your email address if you're logged in) with our live chat provider, Drift. Here's their privacy policy.

If you don't want to do this, you can email us instead at contact@anvil.works.

Show and hide Data Grid rows and columns

Are you new here?

Anvil is a tool for building full-stack web apps with nothing but Python and a drag-and-drop designer. Learn more on our website, or sign up and try it yourself -- it's free!

Data Grid rows and columns can be shown and hidden programmatically.

Try out this app for an example.

Click here to clone the example app in the Anvil designer.

Animation of showing and hiding Data Grid rows/columns

Rows

Hiding/showing a row is simply done by setting the visible property of the row in question.

rows = self.repeating_panel_stocks.get_components()
rows[row_number].visible = False

Columns

Hiding columns is done by modifying the Data Grid’s columns property. This is a list of columns; each column is represented as a dictionary. So to hide a column, remove a column from the list. Add it back to the list to show it again.

To make the change live, you must run the assignment (=) operator for the columns property.

Here’s how you hide a column:

# Filter the column with title 'Stock Price' out of the columns list.
column = [c for c in self.data_grid_1.columns if c['title'] == 'Stock Price'][0]

# Remember the details of the hidden column
self.hidden_columns.append(column)

# Remove it from the Data Grid's column list
self.data_grid_stocks.columns.remove(column)

# Make the change live
self.data_grid_stocks.columns = self.data_grid_stocks.columns

And to show it again, you do the reverse:

# Filter the column with title 'Stock Price' out of the hidden columns list.
column = [c for c in self.hidden_columns if c['title'] == 'Stock Price'][0]

# Remove it from the hidden columns list
self.hidden_columns.remove(column)

# Add it to the Data Grid's column list
self.data_grid_stocks.columns.append(column)

# Make the change live
self.data_grid_stocks.columns = self.data_grid_stocks.columns