我在 DataGridView 中有一个列是 ComboBox,我无法填充它。我一遍又一遍地查看我的代码,这似乎是正确的,但不可能是因为 ComboBox 从未填充过。
这是我的代码。
首先,一个用于测试的静态 DataSource:
List<Phone> PhoneList = new List<Phone>();
Phone p1 = new Phone();
p1.PhoneID = 1;
p1.PhoneTypeID = 2;
p1.AreaCode = "333";
p1.Number = "123-1234";
p1.PhoneTypeName = "Primary";
PhoneList.Add( p1 );
Phone p2 = new Phone();
p2.PhoneID = 2;
p2.PhoneTypeID = 2;
p2.AreaCode = "444";
p2.Number = "432-8900";
p2.PhoneTypeName = "Secondary";
PhoneList.Add( p2 );
这只是为了显示 ComboBox 列的 DataPropertyName:
this.dgvClinic.Columns[ "PhoneName" ].DataPropertyName = "PhoneID";
接下来,我正在提取数据,然后对于它的每一行,我向 DataGridView 添加一行并为新添加的行填充单元格。最后是我的 ComboBox 列,如您所见,我正在填充我的 DataSource、DisplayMember 和 ValueMember 属性。请注意最后三个注释掉的行,因为我什至尝试将静态值添加到单元格的 Item 集合中。两种方法都不起作用。所发生的只是前三列有数据,但 ComboBox 单元格从来没有任何数据。
Clinic c = new Clinic();
string CurrentLocation = string.Empty;
foreach ( Clinic i in c.SelfListAll() )
{
Location_List_ByClinicResult l = i.Locations.FirstOrDefault<Location_List_ByClinicResult>();
if ( l == null )
{
CurrentLocation = "12345 Main Street" + " " + "San Rafael" + ", " + "CA" + " " + "94903";
}
else
{
CurrentLocation = l.Address1 + " " + l.City + ", " + l.StateAbbrev + " " + l.Zip;
}
RowIndex = this.dgvClinic.Rows.Add();
this.dgvClinic.Rows[ RowIndex ].Cells[ "ClinicID" ].Value = i.ClinicID.ToString();
this.dgvClinic.Rows[ RowIndex ].Cells[ "ClinicName" ].Value = i.Name;
this.dgvClinic.Rows[ RowIndex ].Cells[ "LocationName" ].Value = CurrentLocation;
DataGridViewComboBoxCell cell = this.dgvClinic.Rows[ RowIndex ].Cells[ "PhoneName" ] as DataGridViewComboBoxCell;
cell.DataSource = PhoneList;
cell.DisplayMember = "Number";
cell.ValueMember = "PhoneID";
//cell.Items.Add( "One" );
//cell.Items.Add( "Two" );
//cell.Items.Add( "Three" );
}
我真的希望有人能看到我在这里缺少的东西。顺便说一句,我已经在设计器中创建了列。
谢谢。