ComboBox.DataSource Property

Definition

Gets or sets the data source for this ComboBox.

C#
public object DataSource { get; set; }
C#
public object? DataSource { get; set; }

Property Value

An object that implements the IList interface or an Array. The default is null.

Examples

The following example uses the DataSource property to display an array of weekdays in a ComboBox.

C#
using System.Windows.Forms;

namespace ComboBox_DataSource_Sample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            // Initialize an array with data to bind to the combo box.
            var daysOfWeek =
                new[] { "Monday", "Tuesday", "Wednesday", 
                        "Thursday", "Friday", "Saturday", 
                        "Sunday" };

            // Initialize combo box
            var comboBox = new ComboBox
                                {
                                    DataSource = daysOfWeek,
                                    Location = new System.Drawing.Point(12, 12),
                                    Name = "comboBox",
                                    Size = new System.Drawing.Size(166, 21),
                                    DropDownStyle = ComboBoxStyle.DropDownList
                                };

            // Add the combo box to the form.
            this.Controls.Add(comboBox);
        }
    }
}

Remarks

A data source can be a database, a Web service, or an object that can later be used to generate data-bound controls. When the DataSource property is set, the items collection cannot be modified.

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10