using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Windows.Forms; using TradeIdeas.TIQData; using TradeIdeas.MiscSupport; namespace TradeIdeas.TIQ.GUI { public static class KeithMenzelAlertWindow { /// /// This exists for the sole purpose of overriding the GetValue method. /// The rows are dictionaries, and the built in classes are not smart enough to read from a /// dictionary. /// It seems like you could say symbolColumn.DataPropertyName = "[symbol]";. That would work /// in WPF. However, this table isn't as smart. You can only look for simple properties. /// You can't look for an index. /// private class DictionaryStringCell : DataGridViewTextBoxCell { public string DictionaryKey { get; set; } protected override object GetValue(int rowIndex) { if (rowIndex < 0) return null; BindingList list = DataGridView.DataSource as BindingList; if (null == list) return null; if (rowIndex >= list.Count) return null; RowData row = list[rowIndex]; if (null == row) return null; return row.GetAsObject(DictionaryKey); } public override object Clone() { DictionaryStringCell result = base.Clone() as DictionaryStringCell; result.DictionaryKey = DictionaryKey; return result; } } private class PriceStringCell : DictionaryStringCell { private string CachedValue; public PriceStringCell() { Style.Alignment = DataGridViewContentAlignment.MiddleRight; } protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context) { if (null == CachedValue) { try { double asDouble; string fromServer = (string)GetValue(rowIndex); if (null == fromServer) // This is explicitly legal. CachedValue = ""; else if (!Double.TryParse(fromServer, out asDouble)) CachedValue = ""; else CachedValue = asDouble.ToString("0.00"); // 2 places past decimal } catch { CachedValue = "***"; } } return CachedValue; } } public static TraditionalAlertsForm KeithMenzelForm() { TraditionalAlertsForm result = new TraditionalAlertsForm(); DictionaryStringCell timeCellTemplate = new DictionaryStringCell(); timeCellTemplate.DictionaryKey = "time"; DataGridViewColumn timeColumn = new DataGridViewColumn(timeCellTemplate); //timeColumn.DataPropertyName = "[time]"; timeColumn.ReadOnly = true; timeColumn.HeaderText = "Time"; timeColumn.Width = timeColumn.Width / 2; result.DataGridView.Columns.Add(timeColumn); DictionaryStringCell symbolCellTemplate = new DictionaryStringCell(); symbolCellTemplate.DictionaryKey = "symbol"; DataGridViewColumn symbolColumn = new DataGridViewColumn(symbolCellTemplate); //symbolColumn.DataPropertyName = "[symbol]"; symbolColumn.ReadOnly = true; symbolColumn.HeaderText = "Symbol"; symbolColumn.Width = symbolColumn.Width / 2; result.DataGridView.Columns.Add(symbolColumn); PriceStringCell priceCellTemplate = new PriceStringCell(); priceCellTemplate.DictionaryKey = "price"; DataGridViewColumn priceColumn = new DataGridViewColumn(priceCellTemplate); priceColumn.ReadOnly = true; priceColumn.HeaderText = "Price"; priceColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight; priceColumn.Width = priceColumn.Width * 2 / 3; result.DataGridView.Columns.Add(priceColumn); //result.DataGridView.ColumnCount = 2; //result.DataGridView.Columns[0].HeaderText = "Time"; //result.DataGridView.Columns[1].HeaderText = "Symbol"; return result; } public static Form MakeFinal() { TraditionalAlertsForm result = KeithMenzelForm(); result.Text = "Final"; result.Category = "KeithMenzel-hammer-end"; return result; } public static Form MakePreview() { TraditionalAlertsForm result = KeithMenzelForm(); result.Text = "Preview"; result.Category = "KeithMenzel-hammer-preview"; return result; } } }