2013-06-18 27 views
1

我嘗試使用bindingList包裝 字段調用gridview控制器,並且收到類似下面的錯誤消息。字段帶有bindingList包裝的gridview控制器

在所選數據源上找不到名稱爲'CustomerName'的字段或屬性。

我的GridView的代碼是

GridViewSerials.AutoGenerateColumns = false; GridViewSerials.DataSourceID = null; GridViewSerials.DataSource = null; 
CustomersController customersController = new CustomersController(); 
LicenseWrapperCollection licenseWrapperCollection = new Entities.LicenseWrapperCollection(); 
licenseWrapperCollection = customersController.GetAllCustomersAndSerials(); 

GridViewSerials.DataSource = licenseWrapperCollection; BoundField boundFiled  = GridViewSerials.Columns[2] as BoundField; boundFiled.DataField  = "CustomerName";  


GridViewSerials.DataKeyNames[0] = "CustomerId"; 

我的包裝類是

public class LicenseWrapper 
{ 
#region Fields 
private License _license; 
private Customer _customer; 
#endregion 

#region Constructors 

internal LicenseWrapper(License license, Customer customer) 
{ 
    _license = license; 
    _customer = customer; 

    FillWithProperties(); 
} 
#endregion 

#region Properties 
internal string customer_id { get; set; } 
internal string  CustomerId    { get; set; }  
internal string  CustomerName    { get; set; } 
internal string  VatNumber    { get; set; } 
internal string  SerialNumber    { get; set; } 
internal DateTime? InstallationDate   { get; set; } 
internal DateTime? IssueDate    { get; set; } 
internal DateTime? ExpirationDate   { get; set; } 
internal string  HardwareId    { get; set; } 
internal string  CurrentVersion   { get; set; } 
internal bool  LicenseHasBeenDownloaded { get; set; } 
#endregion 

#region Methods 
private void FillWithProperties() 
{ 
    customer_id     = _customer.Id.ToString(); 
    CustomerId     = _customer.Id.ToString(); 
    CustomerName    = string.Format("{0} {1}",_customer.FirstName, _customer.LastName); 
    VatNumber     = _customer.VatNumber; 
    SerialNumber    = _license.SerialNumber; 
    InstallationDate   = DateTime.Now; //must be fixed when fixed database _installation.InstallationDate; 
    IssueDate     = _license.IssueDate; 
    ExpirationDate    = _license.ExpirationDate; 
    HardwareId     = _license.Installation != null ? _license.Installation.HardwareId : string.Empty; 
    LicenseHasBeenDownloaded = _license.Installation != null ? _license.Installation.LicenseHasBeenDownLoaded : false; 
} 

public override string ToString() 
{ 
    return string.Format("{0} {1} {2}", CustomerId, CustomerName, SerialNumber); 
} 
#endregion 

} 

我的錯誤信息是

> A field or property with the name 'CustomerName' was not found on the 
> selected data source. 

如何解決呢? 謝謝

+0

你能顯示產生錯誤的代碼嗎? – Andrei

回答

0

答案和解決方案是:: 使用LinqDataSource和dbml數據類。 所以我們有一個LinqDataClasse => LinqDataSource => GridView的DataClass! 它適合我!