/// <exclude/> /// <summary> /// </summary> /// <param name="cellType"></param> /// <exception cref="NotImplementedException"></exception> DataControlFieldCell IHasCustomCells.CreateCell(DataControlCellType cellType) { switch (cellType) { case DataControlCellType.Header: if (_headerRow == null) { _headerRow = new MatrixHeaderRow(this); return(_headerRow); } return(new MatrixHeaderRowProxy(_headerRow)); case DataControlCellType.DataCell: return(new MatrixRow(this)); case DataControlCellType.Footer: return(_footerRow ?? (_footerRow = new MatrixRow(this))); default: throw new NotImplementedException(); } }
public override void DataBind() { MatrixRow mr = (MatrixRow)this.Parent; MatrixField mf = (MatrixField)mr.ContainingField; Dictionary <string, object> dict = new Dictionary <string, object>(StringComparer.CurrentCultureIgnoreCase); // Grid Row data item fields. Their values will get overwritten with more specific values below // By returning these properties, we enable ItemTemplate to Eval row fields as well. GridViewRow gridRow = (GridViewRow)this.NamingContainer; ICustomTypeDescriptor custom = gridRow.DataItem as ICustomTypeDescriptor; if (custom != null) { // Only add those properties which have not already been added foreach (KeyValuePair <string, object> kvp in custom.GetProperties().Cast <PropertyDescriptor>() .Select(p => new KeyValuePair <string, object>(p.Name, p.GetValue(custom)))) { dict[kvp.Key] = kvp.Value; } } // Header fields for (int i = 0; i < mf.DataHeaderFields.Length; ++i) { dict[mf.DataHeaderFields[i]] = _dc.DataHeaderValues[i]; } // Value fields string formatString; switch (gridRow.RowType) { case DataControlRowType.DataRow: switch (_dc.MatrixColumn.ColumnType) { case MatrixColumnType.CellValue: bool b = mf.CurCellValues.TryGetValue(_dc.DataHeaderValueIndex, out _values); if (b) { if (_dc.MatrixColumn.DisplayColumnTotal) { _dc.UpdateColumnTotals(_values); } } else { _values = new object[mf.DataCellFields.Length]; } break; case MatrixColumnType.RowTotal: int[] rowTotalindexes = mf.DataCellFields .Select((p, i) => _dc.MatrixColumn.DataCellFormatString.Contains("{" + i.ToString()) ? i : -1) .Where(p => p >= 0).ToArray(); var query = from i in Enumerable.Range(0, mf.DataHeaderValues.Count) where mf.CurCellValues.ContainsKey(i) select mf.CurCellValues[i]; decimal?[] rowTotals = new decimal?[mf.DataCellFields.Length]; foreach (object[] val in query) { foreach (int index in rowTotalindexes) { if (!Convert.IsDBNull(val[index])) { decimal d = Convert.ToDecimal(val[index]); rowTotals[index] = (rowTotals[index] ?? 0) + d; } } } _values = rowTotals.Cast <object>().ToArray(); if (_dc.MatrixColumn.DisplayColumnTotal) { _dc.UpdateColumnTotals(_values); } break; default: throw new NotImplementedException(); } if (_dc.MatrixColumn.ItemTemplate == null) { formatString = this.DisplayColumn.MatrixColumn.DataCellFormatString; } else { formatString = null; } break; case DataControlRowType.Footer: _values = _dc.ColumnTotal.Cast <object>().ToArray(); formatString = this.DisplayColumn.MatrixColumn.ColumnTotalFormatString; break; default: throw new NotImplementedException(); } for (int i = 0; i < mf.DataCellFields.Length; ++i) { dict[mf.DataCellFields[i]] = _values[i]; } _coll = new PropertyDescriptorCollection( dict.Select(p => new KeyValuePropertyDescriptor(p.Key, p.Value)).ToArray() ); if (!string.IsNullOrEmpty(formatString)) { // This means that no template has been specified try { this.Text = string.Format(formatString, _values); } catch (FormatException) { // Show diagnostic string msg = string.Format("Format string: {0}; Cell Fields: {1}", formatString, string.Join(", ", mf.DataCellFields)); throw new FormatException(msg); } } base.DataBind(); // Save memory. Coll is no longer needed // Sharad 15 Jun 2011: _coll cannot be made null because app code might be needing it // during the RowDataBound event of the grid. //_coll = null; }