Datagrid Totals in the last row

Hi Community ANVIL:

How do I make a datagrid show the sum of one or two aligned columns in the last row?

Code:

class reporte_partida(reporte_partidaTemplate):
def init(self, fila_partida, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)
self.fila_partida = fila_partida
self.cargar_datagrid()
self.actualizar_componentes()

def cargar_datagrid(self):
if self.fila_partida:
lineas = anvil.server.call(‘obtener_lineas_partida’, self.fila_partida[‘NumPda’], ‘DR’)
self.repeating_panel_1.items = lineas
# Sumar los valores de Débito y Crédito
total_debito = sum(f[“Debito”] for f in self.repeating_panel_1.items)
total_credito = sum(f[“Credito”] for f in self.repeating_panel_1.items)

# Crear la fila total
fila_total = DataRowPanel()
fila_total.add_component(Label(text="TOTALES", bold=True), column="Comentario")
fila_total.add_component(Label(text=str(round(total_debito, 2)), bold=True), column="Debito")
fila_total.add_component(Label(text=str(round(total_credito, 2)), bold=True), column="Credito")

# Agregar la fila al final del DataGrid
self.data_grid_1.add_component(fila_total)

You can create a dummy first column that acts as a total row header for the total row, and after you have added all the other rows to the items property, you can add a final row that takes those values and sums them, keeping the first columns blank:

I would recommend though, that you go with the full datagrid proper rather than manually adding a data panel :slight_smile:

You can add a last_row key to the dictionaries and set it to 1 on the last row. Then you can setup the row template form so it either:

  • hides or shows columns depending on the value of last_row
  • contains two containers, one for the normal rows and one for the totals row, and show one container or the other.

An alternative, valid if you don’t need any fancy formatting, is to use the DataGridJson, which manages the total row for you.

PS
Please format your code so it’s easier to read: How to ask a good question

Hello, thank you for your support. @stefano.menci and @duncan_richards12
I found your help useful, but complicated for my level of knowledge. After trying many options, ChatGPT solved the problem for me. I’m sharing the code for the solution.

PS: Just as an opinion for improvement, it shouldn’t be so complicated for something that is basic and important in reporting, especially for beginners.

  def cargar_datagrid(self):
    if self.fila_partida:
      lineas = list(anvil.server.call('obtener_lineas_partida', self.fila_partida['NumPda'], 'DR'))
  
      # Calcular totales
      total_debito = sum(f["Debito"] or 0 for f in lineas)
      total_credito = sum(f["Credito"] or 0 for f in lineas)
  
      # Crear la fila de totales
      fila_total = {
        "Centro_Costo": None,
        "Cuenta_Contable": {"Codigo": "", "Nombre": "TOTALES"},
        "Comentario": "",
        "Debito": total_debito,
        "Credito": total_credito
      }
  
      # Agregar la fila de totales al final
      lineas.append(fila_total)
  
      # Asignar al repeating panel
      self.repeating_panel_1.items = lineas

You did add one row to the list as I suggested.

You didn’t add one key, so the formatting of the last row is the same as in the other rows, while adding one additional key would allow you to make it bold and change how it looks.

1 Like