Gets or sets the data source whose data contains the possible selections shown in the drop-down list.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Typically this property will be set for an entire column of cells through the DataGridViewComboBoxColumn.DataSource property.
If possible, set 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.
Changing the property causes the cell to reinitialize its Items collection and redraw itself.
When you change the value of the property, the control attempts to use the ValueMember and DisplayMember property values with the new data source, and sets each property to Nothing 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 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 property to Nothing.
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 Function CreateComboBoxColumn() _ As DataGridViewComboBoxColumn Dim column As New DataGridViewComboBoxColumn() With column .DataPropertyName = ColumnName.TitleOfCourtesy.ToString() .HeaderText = ColumnName.TitleOfCourtesy.ToString() .DropDownWidth = 160 .Width = 90 .MaxDropDownItems = 3 .FlatStyle = FlatStyle.Flat End With Return column End Function Private Sub SetAlternateChoicesUsingDataSource( _ ByVal comboboxColumn As DataGridViewComboBoxColumn) With comboboxColumn .DataSource = RetrieveAlternativeTitles() .ValueMember = ColumnName.TitleOfCourtesy.ToString() .DisplayMember = .ValueMember End With End Sub Private Function RetrieveAlternativeTitles() As DataTable Return Populate( _ "SELECT distinct TitleOfCourtesy FROM Employees") End Function Private connectionString As String = _ "Integrated Security=SSPI;Persist Security Info=False;" _ & "Initial Catalog=Northwind;Data Source=localhost" Private Function Populate(ByVal sqlCommand As String) As DataTable Dim northwindConnection As New SqlConnection(connectionString) northwindConnection.Open() Dim command As New SqlCommand(sqlCommand, _ northwindConnection) Dim adapter As New SqlDataAdapter() adapter.SelectCommand = command Dim table As New DataTable() table.Locale = System.Globalization.CultureInfo.InvariantCulture adapter.Fill(table) Return table End Function ' 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 End Enum
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)