在下拉列表中填充我的DataGridView时遇到问题 [英] Having issues getting a dropdown to populate in my DataGridView
问题描述
我有一个DataGridView
,它是从数据集中填充的.但是数据集位于我的业务逻辑层中,我只得到一个IEnumberable,然后从中加载网格视图.
注意:除了LINQ之外,我没有使用任何对我来说有好处的MVC框架或VS功能.
这是我正在使用的POCO类:
I have a DataGridView
that is populated from a dataset. But the dataset is down in my business logic layer and I am getting just an IEnumberable and then am loading up the grid view from that.
Note: I am NOT using any MVC frameworks or any VS features that do nice things for me besides LINQ.
Here are the POCO classes I am using:
public class CertificationType
{
public int Link { get; set; }
public string Name { get; set; }
public PrcCode PrcCode { get; set; }
}
public class PrcCode
{
public int Link { get; set; }
public string AddonName { get; set; }
}
gridview有两列:标题为类型"的DataGridViewTextColumn
和标题为附加代码"的DataGridViewComboBoxColumn
.
以下是填充网格视图的代码(仅假设针对填充POCO类的数据集进行了适当的LINQ查询):
The gridview has two columns: a DataGridViewTextColumn
with a header of "Type," and a DataGridViewComboBoxColumn
with a header of "Add-On Code."
Here is the code to populate the grid view (just assume the proper LINQ queries were made against the data set to populate the POCO classes):
view.addOnCodeColumn.Items.AddRange(
view.addOnCodeColumn.Items.AddRange(
Program.BusinessLayer.GetListOfPrcCodes().
Select(x => x.AddonName).ToArray());
view.typeColumn.DataPropertyName = "Name";
view.certificationTypeGridView.DataSource =
Program.BusinessLayer.GetFullCertificationTypeInfo().
Select(x => new { Name = x.Name, AddonName = x.PrcCode.AddonName }).ToList();
GetListOfPrcCodes()
函数仅返回IEnumerable<PrcCode>
. GetFullCertificationTypeInfo()
类返回一个IEnumerable<CertificationType>
,其中每个CertificationType
POCO的PrcCode
属性都用这样的对象初始化.
结果是,网格在附加代码"旁边显示了正确的文本类型名称,并且它们都在下拉列表中,并且还指定了正确的代码名称,但是当我单击该下拉列表时,它赢得了没开.我做错了什么?
我尝试设置DataSource
属性和匹配的DisplayMember
等属性,并且也遇到了各种讨厌的DataError
.
有人知道我可能做错了什么吗?感谢您的指点!
更新:
我在上面第二个块的末尾添加了一行代码:
The GetListOfPrcCodes()
function just returns an IEnumerable<PrcCode>
. The GetFullCertificationTypeInfo()
class returns an IEnumerable<CertificationType>
with each CertificationType
POCO''s PrcCode
property initialized with an object of such.
What results is that the grid displays the proper textual type names next to the Add-On Codes and they are all in drop downs, and the correct code names are also specified, but then when I go to click the drop-down, it won''t open. What am I doing wrong?
I''ve tried setting the DataSource
property and the matching DisplayMember
etc. properties and running into all kinds of nasty DataError
s too.
Anybody have an idea to what I might be doing incorrectly? Thanks for any pointers!
An update:
I add a line of code to the end of the second block above:
view.certificationTypeGridView.Columns[1].ReadOnly = false;
然后我得到一个InvalidOperationException
:
And then I get an InvalidOperationException
:
DataGridView column bound to a read-only field must have ReadOnly set to True.
推荐答案
更多的谷歌搜索弹出了这篇文章,它挽救了这一天:
^ ]
问题解决了.请很好,感谢我发布我找到的答案,而不是继续看起来像个笨蛋.
Some more googling popped this article and it saved the day:
Clickety[^]
Problem solved. Please be nice and thank me for posting up the answer I found instead of continuing to look like a moron.
这篇关于在下拉列表中填充我的DataGridView时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!