Bind list T to a DataGridView

Here is a technique to bind a List (of objects) to a DataGridView so that the public Properties appear as columns.

BindArrayToGrid

In this example, the columns are binded to the individual public property of the object. The column Random Number is binded to the public property RandomNumber while the column Square Root to the public property SqRt.

Create a class that contains the public properties that appear as columns in the dataGridView

 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestBindArrayToGrid
{
    public class RandomNbr : System.Object
    {
        private Double m_RandomNbr = 0.0;
        private Double m_SqRt = 0.0;

        private Random r;

        public RandomNbr(Double RandomNbr)
        {
            m_RandomNbr = RandomNbr;
            m_SqRt = Math.Sqrt(RandomNbr);
        }

        public RandomNbr()
        {
            m_RandomNbr = GenerateRandomNumber();
            m_SqRt = Math.Sqrt(m_RandomNbr);
        }

        public Double RandomNumber
        {
            get { return this.m_RandomNbr; }
            set { m_RandomNbr = value; }
        }

        public Double SqRt
        {
            get { return this.m_SqRt; }
            set { m_SqRt = value; }
        }

        private double GenerateRandomNumber()
        {
            TimeSpan ts = DateTime.Now.Subtract(new DateTime(2011,01,01));
            var Seconds = ts.TotalSeconds;

            r = new Random((int) Seconds);
            return r.NextDouble();
        }

        public Double GetNextRandom()
        {
            return r.NextDouble();
        }

        public void GetNextRandom(ref double _RandonNbr, ref double _SqRt)
        {
            _RandonNbr = m_RandomNbr = r.NextDouble();
            _SqRt = m_SqRt = Math.Sqrt(m_RandomNbr);
        }

    }
}

And here is the technique to bind the List objects to the DataGridView

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestBindArrayToGrid
{
    public partial class frmBindArrayToGrid : Form
    {
        List<RandomNbr> lstRandom = new List<RandomNbr>();
        private RandomNbr r = new RandomNbr();      // This seeds the random number generator

        public frmBindArrayToGrid()
        {
            InitializeComponent();
        }

        private void cmdExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void frmBindArrayToGrid_FormLoad(object sender, EventArgs e)
        {
            GenerateRandomList();
            BindArrayListToGrid();
        }

        private void BindArrayListToGrid()
        {
            dgRandom.Columns.Clear();

            // Create new column for randon number
            DataGridViewTextBoxColumn csRandom = new DataGridViewTextBoxColumn();
            
            csRandom.DataPropertyName = "RandomNumber";  // Public property name
            csRandom.HeaderText = "Random Number";      // Header name
            csRandom.DefaultCellStyle.Format = "#.#000";    // Format 
            dgRandom.Columns.Add(csRandom);

            // Create a column for the "SqrtValue" property
            // defined in the RandomNumber Class

            DataGridViewTextBoxColumn csSqRt = new DataGridViewTextBoxColumn();

            csSqRt.DataPropertyName = "SqRt";  // Public property name
            csSqRt.HeaderText = "Square Root";      // Header name
            csSqRt.DefaultCellStyle.Format = "#.#000";    // Format 
            dgRandom.Columns.Add(csSqRt);            

            dgRandom.DataSource = lstRandom;        // Binding is to the list of random numbers
            int rowNumber = 1;
            foreach (DataGridViewRow row in dgRandom.Rows)
            {
                row.HeaderCell.Value = "R" + rowNumber;
                rowNumber++;
            }
            dgRandom.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);

            dgRandom.Show();
        }

        private void GenerateRandomList()
        {
            lstRandom.Add(new RandomNbr(r.GetNextRandom()));
            for (int i = 0; i < 100; i++)
            {
                lstRandom.Add(new RandomNbr(r.GetNextRandom()));
            }
        }
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: