![](/img/trans.png)
[英]C# DataGridView.DataSource difference between using BindingSource and not
[英]Populating DataGridView using DataGridView.DataSource property and BindingSource
以下兩個代碼段填充了一個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
});
}
任何想法,將不勝感激。 謝謝。
很難看到,而無需看到QuotesTool.LineItem
,但默認情況下每個成員QuotesTool.LineItem
有用:
[Browsable(false)]
這里的問題通常是前兩個問題之一。 例如,默認情況下,這些都不起作用:
public string Vendor;
internal string Vendor {get;set;}
[Browsable(false)] public string Vendor {get;set;}
但這將:
public string Vendor {get;set;}
請注意,它不必是自動實現的屬性,也不必是可寫的:
private readonly string vendor;
public string Vendor { get { return vendor; } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.