DataGridView ComboBox 永远不会填充
我的 DataGridView 中有一个列是一个 ComboBox,但我无法填充它。我一遍又一遍地查看我的代码,它似乎是正确的,但不可能,因为 ComboBox 从未填充。
这是我的代码。
首先,用于测试的静态数据源:
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" );
}
我真的希望有人能看到我在这里缺少的东西。顺便说一句,我已经在设计器中创建了列。
谢谢。
I have a column in a DataGridView that is a ComboBox, and I cannot get it to populate. I have looked at my code over and over again, and it seems right, but can't be because the ComboBox is never populated.
Here is my code.
First, a static DataSource for testing:
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 );
This just to show the DataPropertyName of the ComboBox column:
this.dgvClinic.Columns[ "PhoneName" ].DataPropertyName = "PhoneID";
Next, I'm pulling data and then for each row of it, I am adding a row to the DataGridView and populating the cell for the newly added row. At the end is my ComboBox column, and as you can see, I'm populating my DataSource, DisplayMember and ValueMember properties. Please notice the three commented out rows at the end as I even tried adding static values to the cell's Item collection. Neither approach worked. All that happens is that the first three columns have data, but the ComboBox cell never has any data.
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" );
}
I'm really hoping that someone can see what I am missing here. By the way, I have created the columns in the designer.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看这部分,我可以观察到您正在将电话列表设置为单元格的数据源。现在,
DisplayMember
和ValueMember
属性将在PhoneList 中查找
。但实际上"Number"
和"PhoneID"
列PhoneList
没有任何名为"Number"
和"PhoneID"
的列,而是 PhoneList 的子 p1 和 p2 有这些列。因此,它将无法在数据源电话列表中找到列("Number"
和"PhoneID"
)。尝试将其填充到数据表中并将其分配给单元格。
现在分配它们。
另请参阅此链接
Taking look at this part, what I can observe is you are setting Phone list as DataSource to cell. Now the
DisplayMember
andValueMember
properties will look for columns"Number"
and"PhoneID"
inPhoneList
. But actuallyPhoneList
doesn't have any columns called"Number"
and"PhoneID"
, rather PhoneList's child p1 and p2 has these columns. So it will fail to find the columns ("Number"
and"PhoneID"
) in the Datasource Phonelist.Try populating it in a datatable and assign it to cell.
and now assign them.
Also see this link
您是否尝试过像这样使用 DataGridViewComboBoxColumn:
也许它的工作方式是组合框应该包含 datagridview 中所有行的相同列表。至少您会知道当您对所有行使用一个 PhoneList 时它是否有效。
Have you tried using DataGridViewComboBoxColumn like this:
Maybe it works in that way that combobox should contain the same list for all rows in a datagridview. At least you will know does it work when you use one PhoneList for all rows.