Click here to Skip to main content
15,892,517 members
Articles / Programming Languages / C#

Leaveraging GenericType Converter for Hybrid Property Databinding

Rate me:
Please Sign up or sign in to vote.
4.73/5 (27 votes)
23 Feb 2008CPOL4 min read 22.7K   131   16  
Leaveraging GenericType Converter for Hybrid Property Databinding
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace HybridProperty
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            BindingList<Option> bl = new BindingList<Option>();
            bl.Add(new Option("OEC", 1.4567, 4.5, 4.1, 0.007, 1000000, 1.4, DateTime.Now, DateTime.Now.AddDays(2)));
            bl.Add(new Option("OEP", 1.5567, 4.5, 4.1, 0.007, 5000000, 1.4, DateTime.Now, DateTime.Now.AddDays(2)));
            bl.Add(new Option("TCC", 1.6567, 4.5, 4.1, 0.007, 1000000, 1.4, DateTime.Now, DateTime.Now.AddDays(2)));
            bl.Add(new Option("UOC", 2.4567, 4.5, 4.1, 0.007, 1000000, 1.4, DateTime.Now, DateTime.Now.AddDays(2)));
            bl.Add(new Option("UOP", 1.4567, -4.5, 4.1, 0.007, -1000000, 1.4, DateTime.Now, DateTime.Now.AddDays(2)));
            bl.Add(new Option("RNGB", 1.567, 4.5, 4.1, 0.007, 1000000, 1.4, DateTime.Now, DateTime.Now.AddDays(2)));
            bl.Add(new Option("RNGT", 1.4567, 4.5, 4.1, 0.007, 1000000, 1.4, DateTime.Now, DateTime.Now.AddDays(2)));
            bl.Add(new Option("TARN", -1.4567, 4.5, 4.1, 0.007, -2000000, 1.4, DateTime.Now, DateTime.Now.AddDays(2)));
            bl.Add(new Option("UIDOC", 1.4567, 4.5, 4.1, 0.007, 1000000, 1.4, DateTime.Now, DateTime.Now.AddDays(2)));
            bl.Add(new Option("UIDIP", 1.4567, 4.5, 4.1, 0.007, 1000000, 1.4, DateTime.Now, DateTime.Now.AddDays(2)));
            bl.Add(new Option("UIDODP", 1.4567, 4.5, 4.1, 0.007, 1000000, 1.4, DateTime.Now, DateTime.Now.AddDays(2)));
            bl.Add(new Option("UIDODC", 1.4567, 4.5, 4.1, 0.007, 1000000, 1.4, DateTime.Now, DateTime.Now.AddDays(2)));
            bl.Add(new Option("UIP", 1.4567, 4.5, 4.1, 0.007, 1000000, 1.4, DateTime.Now, DateTime.Now.AddDays(2)));
            bl.Add(new Option("UODIC", 1.4567, 4.5, 4.1, 0.007, 1000000, 1.4, DateTime.Now, DateTime.Now.AddDays(2)));
            bl.Add(new Option("UODIP", 1.4567, 4.5, 4.1, 0.007, 1000000, 1.4, DateTime.Now, DateTime.Now.AddDays(2)));
            bl.Add(new Option("UODOP", 1.4567, 4.5, 4.1, 0.007, 1000000, 1.4, DateTime.Now, DateTime.Now.AddDays(2)));

            this.dataGridView1.DataSource = bl;
            this.dataGridView1.Columns["Expiry"].DefaultCellStyle.Format = "ddMMMyyddd";
            this.dataGridView1.Columns["Settle"].DefaultCellStyle.Format = "ddMMMyyddd";
            this.dataGridView1.Columns["Spot"].DefaultCellStyle.Format = "0.#####";
            this.dataGridView1.Columns["Vol"].DefaultCellStyle.Format = "0.#####";
            this.dataGridView1.Columns["Yield"].DefaultCellStyle.Format = "0.#####";
            this.dataGridView1.Columns["Swap"].DefaultCellStyle.Format = "0.#####";
            this.dataGridView1.Columns["Notional"].DefaultCellStyle.Format = "#,###0";
            this.dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
        }

        void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.Value.ToString().StartsWith("="))
                return;

            switch (dataGridView1.Columns[e.ColumnIndex].Name)
            {
                case "Settle":
                case "Expiry":
                    // Set the cell ToolTip to the text value.
                    DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
                    cell.ToolTipText = cell.Value.ToString();
                    e.Value = DateTime.Parse(e.Value.ToString()).ToString(e.CellStyle.Format);
                    e.FormattingApplied = true;
                    break;

                case "Spot":
                case "Vol":
                case "Yield":
                    if (double.Parse(e.Value.ToString()) < 0)
                    {
                        e.CellStyle.BackColor = Color.Red;
                        e.CellStyle.SelectionBackColor = Color.DarkRed;
                    }
                    e.Value = double.Parse(e.Value.ToString()).ToString(e.CellStyle.Format, CultureInfo.InvariantCulture);
                    e.FormattingApplied = true;
                    break;

                case "Notional":
                    if (double.Parse(e.Value.ToString()) < 0)
                    {
                        e.CellStyle.BackColor = Color.Blue;
                        e.CellStyle.SelectionBackColor = Color.DarkBlue;
                        dataGridView1.Columns[e.ColumnIndex].HeaderText = dataGridView1.Columns[e.ColumnIndex].Name + " (Sell) ";
                    }
                    e.Value = int.Parse(e.Value.ToString()).ToString(e.CellStyle.Format, CultureInfo.InvariantCulture);
                    e.FormattingApplied = true;
                    dataGridView1.Columns[e.ColumnIndex].HeaderText = dataGridView1.Columns[e.ColumnIndex].Name + " (Buy) ";
                    break;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Alan&Aamy Ltd
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions