Click here to Skip to main content
13,150,539 members (35,142 online)
Click here to Skip to main content

Stats

227.7K views
9.3K downloads
145 bookmarked
Posted 27 Jul 2007
BSD

A data-bound multi-column combobox

, 27 Jul 2007
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

Share

About the Author

Nish Nishant
United States United States
Nish Nishant is the Principal Software Architect/Consultant for Ganymede Software Solutions LLC, and is based out of Columbus, Ohio. He has over 17 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish was a Microsoft Visual C++ MVP between 2002 and 2015.

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored C++/CLI in Action for Manning Publications in 2005, and had previously 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 vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : If you are interested in hiring Nish as a consultant, you can reach him via his google email id voidnish.

Company Website : www.ganymedesoftwaresolutions.com

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170924.2 | Last Updated 27 Jul 2007
Article Copyright 2007 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid