What is the best way to create a total/subtotal row in the grid?
I have a grid that shows allocation to different assets in the portfolio. The allocation column is editable.
Need to show the total amount allocated and be able to update it when allocations change or deleted.
Thank you
Hi Aleksandr,
Sure. I'm assuming you're using our DataSourceHelpers, correct? I also believe you're using C#, so i provided my answer in that language. If you need it in Obj-c just let me know.
But basically all you're going to do is override the DSH and tell it if you have one more row to display than the amount of data you have. (Of course you can skip all of that if you just want to add one additional row of data to your underlying data)
I've attached a sample that shows exactly what i'm talking about though.
Let me know if this helps.
-SteveZ
Thank you. This works - almost. My data source was editable before but now that I switched to this code the data source does not seem to allow for editing.
My DataSource (almost a copy from yours is )
public override IGGridViewCell CreateCell (IGGridView gridView, IGCellPath path) { IGCellPath normPath = this.NormalizePath (path); IGGridViewColumnDefinition colDef = (IGGridViewColumnDefinition)this.Columns [normPath.ColumnIndex]; if (colDef != null) { IGGridViewCell cell = colDef.CreateCell (gridView, path, this); if (normPath.RowIndex == this.Data.Length) { if (colDef.FieldKey.Equals ("Weight")) { double tot = this.Data.Select (x => x as NSAlloc).Sum (x => x.Weight.DoubleValue); cell.TextLabel.Text = tot.ToString ("0.00%"); } if (colDef.FieldKey.Equals ("CRisk")) { double tot = this.Data.Select (x => x as NSAlloc).Sum (x => x.CRisk.DoubleValue); cell.TextLabel.Text = tot.ToString ("0.00%"); } if (path.ColumnIndex == 1) { cell.TextLabel.Text = "Total:"; } } return cell; } return null; }
I also set the delegate that allows for editing
AllocationEditDelegate editDel = new AllocationEditDelegate ();
this._allocGridSource.EditingDelegate = editDel; this._allocGrid.DataSource = this._allocGridSource; this._allocGrid.ReloadData ();
Please let me know what I am doing wrong.
Thats my bad, I excluded the editing code from the createCell method before:
Should look like this:
public override IGGridViewCell CreateCell (IGGridView gridView, IGCellPath path) { IGCellPath normPath = this.NormalizePath (path); IGGridViewColumnDefinition colDef = (IGGridViewColumnDefinition)this.Columns[normPath.ColumnIndex]; if(colDef != null) { IGGridViewCell cell = colDef.CreateCell (gridView, path, this); if(this.AllowEditing && colDef.Editable) { UITapGestureRecognizer tap = new UITapGestureRecognizer (this, new Selector ("doubleTap:")); tap.NumberOfTapsRequired = 2; cell.RegisterGestures(new NSObject[]{tap}); } if (normPath.RowIndex == this.Data.Length) { if (colDef.FieldKey == "yearToDateSales") cell.TextLabel.Text = _total.ToString (); } return cell; } return null; }