C# Winform中DataGridView的DataGridViewComboBoxColumn使用方法

时间:2021-09-16 14:45:36
下面介绍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  =   " --请选择-- " ;