手动添加数据源时DataGridViewComboBoxCell值出问题解决方法

给DataGridView更新数据源,采用手动方式,即首先设置手工添加不同的DataGridViewColumn。将各列DataPropertyName属性设置成数据源,就是数据库的列名或者对象名。

dataGridView1.AutoGenerateColumns = false;

DataGridViewComboBoxCell 设置的复杂一些,要设置下拉选项。参考http://technet.microsoft.com/zh-cn/library/ms404353

DataGridViewComboBoxColumn assignedToColumn = 
new DataGridViewComboBoxColumn();

// Populate the combo box drop-down list with Employee objects.
foreach (Employee e in employees) assignedToColumn.Items.Add(e);

// Add "unassigned" to the drop-down list and display it for
// empty AssignedTo values or when the user presses CTRL+0.
assignedToColumn.Items.Add("unassigned");
assignedToColumn.DefaultCellStyle.NullValue = "unassigned";

assignedToColumn.Name = "Assigned To";
assignedToColumn.DataPropertyName = "AssignedTo";
assignedToColumn.AutoComplete = true;
assignedToColumn.DisplayMember = "Name";
assignedToColumn.ValueMember = "Self";

 

你可能感兴趣的:(datagridview)