Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datagrid: Calculate Avg or Sum for column in Footer

I have a datagrid getting bound to a dataset, and I want to display the average result in the footer for a column populated with integers.

The way I figure, there's 2 ways I can think of:

1."Use the Source, Luke"
In the code where I'm calling DataGrid.DataBind(), use the DataTable.Compute() method (or in my case DataSet.DataTable(0).Compute()). For example:

Dim strAverage = DataTable.Compute("Avg(ColumnName)", "")  

But once I have this, how can I insert it into the footer?

2."Bound for Glory"
Using the DataGrid.ItemDataBound event, and calculating a running total from every ListItemType.Item and ListItemType.AlternatingItem, finally displaying in ListItemType.Footer. For example:

Select Case e.Item.ItemType
    Case ListItemType.Item, ListItemType.AlternatingItem
        runningTotal += CInt(e.Item.Cells(2).Text)
    Case ListItemType.Footer
        e.Item.Cells(2).Text = runningTotal/DataGrid.Items.Count
End Select

This just feels wrong, plus I would have to make sure the runningTotal is reset on every DataBind.

Is there a better way?

like image 348
Mark Glorie Avatar asked Nov 05 '22 23:11

Mark Glorie


1 Answers

I don't know if either are necessarily better, but two alternate ways would be:

  1. Manually run through the table once you hit the footer and calculate from the on-screen text
  2. Manually retrieve the data and do the calculation separately from the bind

Of course, #2 sort of offsets the advantages of data binding (assuming that's what you're doing).

like image 56
TheSmurf Avatar answered Nov 14 '22 22:11

TheSmurf