Export (0) Print
Expand All

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 System.Windows.Forms.dll)

abstract DataSource : Object with get, set 
override DataSource : Object with get, set

Property Value

Type: System.Object
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).

ExceptionCondition
ArgumentException

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.

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

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.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft