Permalink
Fetching contributors…
Cannot retrieve contributors at this time
89 lines (66 sloc) 7.97 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
How to: Access Objects in a Windows Forms DataGridViewComboBoxCell Drop-Down List
03/30/2017
.net-framework
dotnet-winforms
article
jsharp
DataGridView control [Windows Forms], accessing objects in combo box cells
combo boxes, in DataGridView control
combo boxes, accessing objects in DataGridViewComboBoxCell drop-down lists
bcbe794a-d1fa-47f8-b5a3-5f085b32097d
5
dotnet-bot
dotnetcontent
wpickett

How to: Access Objects in a Windows Forms DataGridViewComboBoxCell Drop-Down List

Like the xref:System.Windows.Forms.ComboBox control, the xref:System.Windows.Forms.DataGridViewComboBoxColumn and xref:System.Windows.Forms.DataGridViewComboBoxCell types enable you to add arbitrary objects to their drop-down lists. With this feature, you can represent complex states in a drop-down list without having to store corresponding objects in a separate collection.

Unlike the xref:System.Windows.Forms.ComboBox control, the xref:System.Windows.Forms.DataGridView types do not have a xref:System.Windows.Forms.ComboBox.SelectedItem%2A property for retrieving the currently selected object. Instead, you must set the xref:System.Windows.Forms.DataGridViewComboBoxColumn.ValueMember%2A?displayProperty=nameWithType or xref:System.Windows.Forms.DataGridViewComboBoxCell.ValueMember%2A?displayProperty=nameWithType property to the name of a property on your business object. When the user makes a selection, the indicated property of the business object sets the cell xref:System.Windows.Forms.DataGridViewCell.Value%2A property.

To retrieve the business object through the cell value, the ValueMember property must indicate a property that returns a reference to the business object itself. Therefore, if the type of the business object is not under your control, you must add such a property by extending the type through inheritance.

The following procedures demonstrate how to populate a drop-down list with business objects and retrieve the objects through the cell xref:System.Windows.Forms.DataGridViewCell.Value%2A property.

To add business objects to the drop-down list

  1. Create a new xref:System.Windows.Forms.DataGridViewComboBoxColumn and populate its xref:System.Windows.Forms.DataGridViewComboBoxColumn.Items%2A collection. Alternatively, you can set the column xref:System.Windows.Forms.DataGridViewComboBoxColumn.DataSource%2A property to the collection of business objects. In that case, however, you cannot add "unassigned" to the drop-down list without creating a corresponding business object in your collection.

    [!code-csharpSystem.Windows.Forms.DataGridViewComboBoxObjectBinding#110] [!code-vbSystem.Windows.Forms.DataGridViewComboBoxObjectBinding#110]

  2. Set the xref:System.Windows.Forms.DataGridViewComboBoxColumn.DisplayMember%2A and xref:System.Windows.Forms.DataGridViewComboBoxColumn.ValueMember%2A properties. xref:System.Windows.Forms.DataGridViewComboBoxColumn.DisplayMember%2A indicates the property of the business object to display in the drop-down list. xref:System.Windows.Forms.DataGridViewComboBoxColumn.ValueMember%2A indicates the property that returns a reference to the business object.

    [!code-csharpSystem.Windows.Forms.DataGridViewComboBoxObjectBinding#115] [!code-vbSystem.Windows.Forms.DataGridViewComboBoxObjectBinding#115]

  3. Make sure that your business object type contains a property that returns a reference to the current instance. This property must be named with the value assigned to xref:System.Windows.Forms.DataGridViewComboBoxColumn.ValueMember%2A in the previous step.

    [!code-csharpSystem.Windows.Forms.DataGridViewComboBoxObjectBinding#310] [!code-vbSystem.Windows.Forms.DataGridViewComboBoxObjectBinding#310]

To retrieve the currently selected business object

Example

The complete example demonstrates the use of business objects in a drop-down list. In the example, a xref:System.Windows.Forms.DataGridView control is bound to a collection of Task objects. Each Task object has an AssignedTo property that indicates the Employee object currently assigned to that task. The Assigned To column displays the Name property value for each assigned employee, or "unassigned" if the Task.AssignedTo property value is null.

To view the behavior of this example, perform the following steps:

  1. Change assignments in the Assigned To column by selecting different values from the drop-down lists or pressing CTRL+0 in a combo-box cell.

  2. Click Generate Report to display the current assignments. This demonstrates that a change in the Assigned To column automatically updates the tasks collection.

  3. Click a Request Status button to call the RequestStatus method of the current Employee object for that row. This demonstrates that the selected object has been successfully retrieved.

[!code-csharpSystem.Windows.Forms.DataGridViewComboBoxObjectBinding#000] [!code-vbSystem.Windows.Forms.DataGridViewComboBoxObjectBinding#000]

Compiling the Code

This example requires:

  • References to the System and System.Windows.Forms assemblies.

See Also

xref:System.Windows.Forms.DataGridView
xref:System.Windows.Forms.DataGridViewComboBoxColumn
xref:System.Windows.Forms.DataGridViewComboBoxColumn.Items%2A?displayProperty=nameWithType
xref:System.Windows.Forms.DataGridViewComboBoxColumn.DataSource%2A?displayProperty=nameWithType
xref:System.Windows.Forms.DataGridViewComboBoxColumn.ValueMember%2A?displayProperty=nameWithType
xref:System.Windows.Forms.DataGridViewComboBoxCell
xref:System.Windows.Forms.DataGridViewComboBoxCell.Items%2A?displayProperty=nameWithType
xref:System.Windows.Forms.DataGridViewComboBoxCell.DataSource%2A?displayProperty=nameWithType
xref:System.Windows.Forms.DataGridViewComboBoxCell.ValueMember%2A?displayProperty=nameWithType
xref:System.Windows.Forms.DataGridViewCell.Value%2A?displayProperty=nameWithType
xref:System.Windows.Forms.ComboBox
Displaying Data in the Windows Forms DataGridView Control