Click here to Skip to main content
15,888,984 members
Articles / Desktop Programming / Windows Forms

A data-bound multi-column combobox

Rate me:
Please Sign up or sign in to vote.
4.90/5 (76 votes)
27 Jul 2007BSD4 min read 343.4K   12.1K   151  
An ownerdrawn multi-column combobox class with support for data-binding
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MultiColumnComboBoxDemo
{
    public partial class DemoForm : Form
    {
        public DemoForm()
        {
            InitializeComponent();

            SetupData();
        }

        private void SetupData()
        {
            // Populate using a DataTable

            DataTable dataTable = new DataTable("Employees");

            dataTable.Columns.Add("Employee ID", typeof(String));
            dataTable.Columns.Add("Name", typeof(String));
            dataTable.Columns.Add("Designation", typeof(String));

            dataTable.Rows.Add(new String[] { "D1", "Natalia", "Developer" });
            dataTable.Rows.Add(new String[] { "D2", "Jonathan", "Developer" });
            dataTable.Rows.Add(new String[] { "D3", "Jake", "Developer" });
            dataTable.Rows.Add(new String[] { "D4", "Abraham", "Developer" });
            dataTable.Rows.Add(new String[] { "T1", "Mary", "Team Lead" });
            dataTable.Rows.Add(new String[] { "PM1", "Calvin", "Project Manager" });
            dataTable.Rows.Add(new String[] { "T2", "Sarah", "Team Lead" });
            dataTable.Rows.Add(new String[] { "D12", "Monica", "Developer" });
            dataTable.Rows.Add(new String[] { "D13", "Donna", "Developer" });

            multiColumnComboBox1.DataSource = dataTable;
            multiColumnComboBox1.DisplayMember = "Employee ID";
            multiColumnComboBox1.ValueMember = "Name";

            // Populate using a collection

            Student[] studentArray = new Student[] 
            { new Student("Andrew White", 10), new Student("Thomas Smith", 10), new Student("Alice Brown", 11),
              new Student("Lana Jones", 10), new Student("Jason Smith", 9), new Student("Amamda Williams", 11)
            };

            multiColumnComboBox2.DataSource = studentArray;
            multiColumnComboBox2.DisplayMember = multiColumnComboBox2.ValueMember = "Name";

            // Drop-down list (non-editable)

            List<Student> studentList = new List<Student>(studentArray);

            multiColumnComboBox3.DataSource = studentList;

            // Trying to use as a regular combobox

            multiColumnComboBox4.Items.Add("Cat");
            multiColumnComboBox4.Items.Add("Tiger");
            multiColumnComboBox4.Items.Add("Lion");
            multiColumnComboBox4.Items.Add("Cheetah");
            multiColumnComboBox4.SelectedIndex = 0;
        }

        public class Student
        {
            public Student(String name, int age)
            {
                this.name = name;
                this.age = age;
            }

            String name;

            public String Name
            {
                get { return name; }
            }

            int age;

            public int Age
            {
                get { return age; }
            }
        }
    }
}

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 BSD License


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions