private void Position_PropertyChanged(object sender, PropertyChangedEventArgs e) { Position position = (Position)sender; DataRow dataRow = this._dataTable.Rows.Find(position.Ticker); object newValue = dataRow[e.PropertyName] = position[e.PropertyName] ?? DBNull.Value; int index = this._rowChanges.BinarySearchByValue(position.Ticker, change => (string)change.RowKey); RowColumnsChanged rowChange; if (index < 0) { this._rowChanges.Insert(~index, rowChange = new RowColumnsChanged { RowKey = position.Ticker }); } else { rowChange = this._rowChanges[index]; } int columnOrdinal = PositionData.SchemaTable.Columns.IndexOf(e.PropertyName); if ((index = rowChange.ColumnChanges.BinarySearchByValue(columnOrdinal, columnChange => columnChange.ColumnOrdinal)) < 0) { rowChange.ColumnChanges.Insert(~index, new ColumnChange { ColumnOrdinal = columnOrdinal, Value = newValue }); } else { rowChange.ColumnChanges[index].Value = newValue; } }
private void PortfolioData_DataChanged(object sender, DataEventArgs <RowChangeBase[]> e) { PortfolioData portfolioData = (PortfolioData)sender; decimal portfolioPnl; lock (this._summary) { decimal[] pnl = this._pnls[portfolioData.PortfolioId]; DataRow summaryRow = this._summary.Rows.Find(portfolioData.PortfolioId); portfolioPnl = summaryRow.Field <decimal>("Pnl"); int portfolioDataPnlOrdinal = PositionData.SchemaTable.Columns.IndexOf("Pnl"); foreach (RowChangeBase rowChange in e.Data) { string ticker = (string)rowChange.RowKey; int index = Array.BinarySearch(this._tickers, ticker); if (rowChange.ChangeType == RowChangeType.Added) { decimal rowPnl = (decimal)((RowAdded)rowChange).Data[portfolioDataPnlOrdinal]; portfolioPnl += (pnl[index] = rowPnl); } else { decimal currentRowPnl = pnl[index]; switch (rowChange.ChangeType) { case RowChangeType.Removed: portfolioPnl -= currentRowPnl; pnl[index] = 0m; break; case RowChangeType.ColumnsChanged: ColumnChange columnChange = ((RowColumnsChanged)rowChange).ColumnChanges.FirstOrDefault(change => change.ColumnOrdinal == portfolioDataPnlOrdinal); if (columnChange == null) { continue; } decimal newRowPnl = (decimal)columnChange.Value; pnl[index] = newRowPnl; portfolioPnl += newRowPnl - currentRowPnl; break; } } } summaryRow.SetField("Pnl", portfolioPnl); summaryRow.AcceptChanges(); } lock (this._rowChanges) { int index = this._rowChanges.BinarySearchByValue(portfolioData.PortfolioId, change => (string)change.RowKey); if (index < 0) { this._rowChanges.Insert(~index, new RowColumnsChanged { RowKey = portfolioData.PortfolioId, ColumnChanges = { new ColumnChange { ColumnOrdinal = this._summary.Columns.IndexOf("Pnl"), Value = portfolioPnl } } }); } else { RowColumnsChanged rowChange = (RowColumnsChanged)this._rowChanges[index]; rowChange.ColumnChanges[0].Value = portfolioPnl; } } }