Monday, July 30, 2012

Code DataGridView with Drop-downs from DataTable

This is how to create a DataGridView in code from a DataTable and featuring dropdown columns. Simply create a new Windows Form project and replace the form.cs code with the following:

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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        BindingSource bindingsource1 = new BindingSource();
        BindingSource productbs = new BindingSource();
        System.Windows.Forms.DataGridView dataGridView1;
         DataTable dt;
        public Form1()
        {
            dt = new DataTable();
            var dcblah = new DataColumn("blah");
            dt.Columns.Add(dcblah);
            dt.Columns.Add(new DataColumn("2"));
            DataRow dr = dt.NewRow();
            dr["blah"] = "bii";
            dr["2"] = "boo";
            dt.Rows.Add(dr);
            DataRow drd2 = dt.NewRow();
            drd2["blah"] = "boo";
            drd2["2"] = "bii";
            dt.Rows.Add(drd2);

            this.SuspendLayout();

            MyInitializeComponent();
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;

            var dtp = new DataTable();
            dtp.Columns.Add(new DataColumn("Product_name"));
            dtp.Columns.Add(new DataColumn("blah"));
            var dr1 = dtp.NewRow();
            dr1["blah"] = "boo";
            dr1["Product_name"] = "a product boo";
            var dr2 = dtp.NewRow();
            dr2["blah"] = "bii";
            dr2["Product_name"] = "a xxproduct xxboo";
            dtp.Rows.Add(dr1); dtp.Rows.Add(dr2);
            productbs.DataSource = dtp;

            
            dataGridView1.DataSource = bindingsource1;
            bindingsource1.DataSource = dt;
            
            foreach (DataColumn dc in dt.Columns)
            {
                ((DataGridViewComboBoxColumn)dataGridView1.Columns[dc.ColumnName]).DataPropertyName = dc.ColumnName;
                ((DataGridViewComboBoxColumn)dataGridView1.Columns[dc.ColumnName]).DataSource = productbs;
                ((DataGridViewComboBoxColumn)dataGridView1.Columns[dc.ColumnName]).DisplayMember = "Product_name";
                ((DataGridViewComboBoxColumn)dataGridView1.Columns[dc.ColumnName]).ValueMember = "blah";
            }
 
            this.Controls.Add(dataGridView1);
        }
        private void MyInitializeComponent()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            foreach (DataColumn dc in dt.Columns)
            {
                var col = new DataGridViewComboBoxColumn();
                col.HeaderText = dc.ColumnName;
                col.Name = dc.ColumnName;
                dataGridView1.Columns.Add(col);
            }
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.dataGridView1.Location = new System.Drawing.Point(22, 97);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(240, 150);
            this.dataGridView1.TabIndex = 0;
 
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }


    }
}