DataGridViewComboBoxCell.DataSource Property

Definition

Gets or sets the data source whose data contains the possible selections shown in the drop-down list.

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

Property Value

An IList or IListSource that contains a collection of values used to supply data to the drop-down list. The default value is null.

Exceptions

The specified value when setting this property is not null and is not of type IList nor IListSource.

Examples

The following code example demonstrates the use of the DataGridViewComboBoxColumn.DataSource property, which is similar to this property. This example is part of a larger example available in the DataGridViewComboBoxColumn class overview topic.

C#
private DataGridViewComboBoxColumn CreateComboBoxColumn()
{
    DataGridViewComboBoxColumn column =
        new DataGridViewComboBoxColumn();
    {
        column.DataPropertyName = ColumnName.TitleOfCourtesy.ToString();
        column.HeaderText = ColumnName.TitleOfCourtesy.ToString();
        column.DropDownWidth = 160;
        column.Width = 90;
        column.MaxDropDownItems = 3;
        column.FlatStyle = FlatStyle.Flat;
    }
    return column;
}

private void SetAlternateChoicesUsingDataSource(DataGridViewComboBoxColumn comboboxColumn)
{
    {
        comboboxColumn.DataSource = RetrieveAlternativeTitles();
        comboboxColumn.ValueMember = ColumnName.TitleOfCourtesy.ToString();
        comboboxColumn.DisplayMember = comboboxColumn.ValueMember;
    }
}

private DataTable RetrieveAlternativeTitles()
{
    return Populate("SELECT distinct TitleOfCourtesy FROM Employees");
}

string connectionString =
    "Integrated Security=SSPI;Persist Security Info=False;" +
    "Initial Catalog=Northwind;Data Source=localhost";

private DataTable Populate(string sqlCommand)
{
    SqlConnection northwindConnection = new SqlConnection(connectionString);
    northwindConnection.Open();

    SqlCommand command = new SqlCommand(sqlCommand, northwindConnection);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = command;

    DataTable table = new DataTable();
    table.Locale = System.Globalization.CultureInfo.InvariantCulture;
    adapter.Fill(table);

    return table;
}

// Using an enum provides some abstraction between column index
// and column name along with compile time checking, and gives
// a handy place to store the column names.
enum ColumnName
{
    EmployeeId,
    LastName,
    FirstName,
    Title,
    TitleOfCourtesy,
    BirthDate,
    HireDate,
    Address,
    City,
    Region,
    PostalCode,
    Country,
    HomePhone,
    Extension,
    Photo,
    Notes,
    ReportsTo,
    PhotoPath,
    OutOfOffice
};

Remarks

Typically this property is set for an entire column of cells through the DataGridViewComboBoxColumn.DataSource property.

If possible, set DataSource to a source containing only the possible selections, like a column of selections. Then the DisplayMember property doesn't need to be set. But if the source is more complicated, set DisplayMember to the name of the property or column from which to retrieve possible selections.

If DataSource is set to a string array, then ValueMember and DisplayMember do not need to be set because each string in the array will be used for both value and display.

Changing the DataSource property causes the cell to reinitialize its Items collection and redraw itself.

When you change the value of the DataSource property, the control attempts to use the ValueMember and DisplayMember property values with the new data source, and sets each property to null if its value cannot be found. Any exception that occurs during this process is ignored unless it is one of the following critical exceptions: NullReferenceException, StackOverflowException, OutOfMemoryException, ThreadAbortException, ExecutionEngineException, IndexOutOfRangeException, or AccessViolationException.

When the DataSource is set, the DataGridViewComboBoxCell attaches to the Disposed event of the data source. When the DataGridViewComboBoxCell is detached from the DataGridView, the resource is not immediately released. The DataGridViewComboBoxCell resource is released when the attached data source is disposed. To release the resource for garbage collection immediately, set the DataSource property to null.

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

See also