使用 DataGridView.DataSource 属性和 BindingSource 填充 DataGridView
以下两个代码片段填充 BindingSource,稍后将其分配给 DataGridView.DataSource。
当使用具体类 QuotesTool.LineItem 时(第一个片段),网格不会显示适当的数据:
BindingSource lineList = new BindingSource();
foreach (XElement y in _lines.Elements())
{
lineList.Add(new QuotesTool.LineItem(
y.Element("Vendor").Value,
y.Element("Model").Value,
y.Element("Selling_Unit").Value,
y.Element("Net_Price").Value,
y.Element("Spec").Value
));
}
但是,如果使用匿名类型,网格显示数据正常:
foreach (XElement y in _lines.Elements())
{
lineList.Add(
new {
vendor = y.Element("Vendor").Value,
Model = y.Element("Model").Value,
UOM = y.Element("Selling_Unit").Value,
Price = y.Element("Net_Price").Value,
Description = y.Element("Spec").Value
});
}
任何想法将不胜感激。谢谢。
The following two code snippets populate a BindingSource which is later assigned to
a DataGridView.DataSource.
When the concrete class QuotesTool.LineItem is used (first snippet) the grid DOES NOT display the appropriate data:
BindingSource lineList = new BindingSource();
foreach (XElement y in _lines.Elements())
{
lineList.Add(new QuotesTool.LineItem(
y.Element("Vendor").Value,
y.Element("Model").Value,
y.Element("Selling_Unit").Value,
y.Element("Net_Price").Value,
y.Element("Spec").Value
));
}
But, if an anonymous type is used the grid displays data OK:
foreach (XElement y in _lines.Elements())
{
lineList.Add(
new {
vendor = y.Element("Vendor").Value,
Model = y.Element("Model").Value,
UOM = y.Element("Selling_Unit").Value,
Price = y.Element("Net_Price").Value,
Description = y.Element("Spec").Value
});
}
Any ideas would be appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有看到
QuotesTool.LineItem
很难判断,但是默认情况下很有用,每个成员:这里的问题通常是前两个问题之一。例如,默认情况下,这些都不起作用:
但这将:
请注意,它不必是自动实现的属性,也不需要可写:
Hard to tell without seeing
QuotesTool.LineItem
, but by default to be useful, each member:[Browsable(false)]
The issue here is usually one of the first two. For example, none of these will work by default:
but this will:
Note that it doesn't have to be an automatically implemented property, nor does it need to be writeable: