C# DataGridView - ComboBox Column Selection Event

July 2, 2017 Oclemy C# DataGridView, C# ComboBox 1 minute, 21 seconds

We are using a datagridview with combobox column.

Our purpose here is to see how to handle the selectedIndex changed of a combobox in our combobox column.

First obviously is to fill our datagridview and combobox with some data.Right now our data source is a simple arraylist. Here's the code :  

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

namespace Combo_Column_Events
{
    public partial class Form1 : Form
    {
        ComboBox combo;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            adddata();

        }

        private void adddata()
        {
            //ADD COLUMNS
            dataGridView1.ColumnCount = 3;
            dataGridView1.Columns[0].Name = "Position";
            dataGridView1.Columns[1].Name = "Team";
            dataGridView1.Columns[2].Name = "Points";

            //ADD ROWS
            ArrayList row = new ArrayList();
            row.Add("1");
            row.Add("PSG");
            row.Add("78");
            dataGridView1.Rows.Add(row.ToArray());

            //2ND ROW
            row = new ArrayList();
            row.Add("2");
            row.Add("LYON");
            row.Add("72");
            dataGridView1.Rows.Add(row.ToArray());

            //3RD ROW
            row = new ArrayList();
            row.Add("3");
            row.Add("Marseille");
            row.Add("70");
            dataGridView1.Rows.Add(row.ToArray());

            //ADD COMBOBOX COLUMN
            DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
            combo.HeaderText = "Years Won";
            combo.Name = "combo";
            row = new ArrayList();
            row.AddRange(new String[] { "2015", "2012", "2010", "1999" });
            combo.Items.AddRange(row.ToArray());

            //ADD THE COLUMN  TO DATAGRIDVIEW
            dataGridView1.Columns.Add(combo);
        }

        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            //GET OUR COMBO OBJECT
            combo = e.Control as ComboBox;
            if(combo != null)
            {
                // AVOID ATTACHMENT TO MULTIPLE EVENT HANDLERS
                combo.SelectedIndexChanged -= new EventHandler(combo_SelectedIndexChanged);

                //THEN NOW ADD
                combo.SelectedIndexChanged +=combo_SelectedIndexChanged;
            }
        }

        private void combo_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selected = (sender as ComboBox).SelectedItem.ToString();
            MessageBox.Show(selected);
        }

    }
}

Cheers.

Comments