下面介紹Winform中DataGridView的DataGridViewComboBoxColumn的使用方法:
DataGridViewComboBoxColumn 手動綁定數據源
1 //默認將AllowUserToAddRows設置成了False,所以創建一個DataGridViewRow添加到DataGridView中
2 DataGridViewRow dgvr = new DataGridViewRow();
3 dgvDownloadList.Rows.Add(dgvr);
4
5 //獲取數據源
6 List<ServiceInterface.dmsManageWCFService.dmsDocumentInfo> DIList = PDMService.DmsManageWcfServiceJoint.GetAllHistroyVersion(item.DocGUID);
7
8 DataTable dtVersion = new DataTable();
9 DataColumn dcVersion = new DataColumn("Version");//顯示Name
10 DataColumn dcVerGUID = new DataColumn("VerGUID");//綁定的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 //綁定初始值顯示Name,就要綁定Value
24 ((DataGridViewComboBoxCell)dgvDownloadList.Rows[0].Cells["Version"]).Value = item.DocVerGuid;
25
26 //綁定默認空值
27 ((DataGridViewComboBoxColumn)dgvDownloadList.Columns["Version"]).DefaultCellStyle.NullValue = "--請選擇--";