The following describes how to use the DataGridViewComboBoxColumn of the DataGridView in Winform:
DataGridViewComboBoxColumnManually bind data sources
1 // by default, AllowUserToAddRows is set to False. Therefore, create a DataGridViewRow and add it to the DataGridView.
2 DataGridViewRow dgvr = new DataGridViewRow ();
3 dgvDownloadList. Rows. Add (dgvr );
4
5 // obtain the data source
6 List <ServiceInterface. dmsManageWCFService. dmsDocumentInfo> DIList = PDMService. DmsManageWcfServiceJoint. GetAllHistroyVersion (item. DocGUID );
7
8 DataTable dtVersion = new DataTable ();
9 DataColumn dcVersion = new DataColumn ("Version"); // display Name
10 DataColumn dcVerGUID = new DataColumn ("VerGUID"); // bound Value
11 dtVersion. Columns. Add (dcVersion );
12 dtVersion. Columns. Add (dcVerGUID );
13
14 foreach (ServiceInterface. dmsManageWCFService. dmsDocumentInfo cVerDoc in DIList)
15 {
16 dtVersion. Rows. Add (cVerDoc. DocVer, cVerDoc. VerGUID );
17}
18
19 (DataGridViewComboBoxCell) dgvDownloadList. Rows [0]. Cells ["Version"]). DataSource = dtVersion;
20 (DataGridViewComboBoxCell) dgvDownloadList. Rows [0]. Cells ["Version"]). ValueMember = "VerGUID ";
21 (DataGridViewComboBoxCell) dgvDownloadList. Rows [0]. Cells ["Version"]). DisplayMember = "Version ";
22
23 // bind the initial Value to display the Name, and bind the Value
24 (DataGridViewComboBoxCell) dgvDownloadList. Rows [0]. Cells ["Version"]). Value = item. DocVerGuid;
25
26 // bind the default null value
27 (DataGridViewComboBoxColumn) dgvDownloadList. Columns ["Version"]). DefaultCellStyle. NullValue = "-- select --";