DataGridViewComboBoxCell.DataSource Property

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

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in

public virtual Object DataSource { get; set; }
/** @property */
public Object get_DataSource ()

/** @property */
public void set_DataSource (Object value)

public function get DataSource () : Object

public function set DataSource (value : Object)

Not applicable.

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 a null reference (Nothing in Visual Basic).

Exception typeCondition


The specified value when setting this property is not a null reference (Nothing in Visual Basic) and is not of type IList nor IListSource.

Typically this property will be 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 a null reference (Nothing in Visual Basic) 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.

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.

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);

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

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

    return table;

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0