2012-02-24 36 views
1

我有一個WCF服務,用於從生成的實體數據模型edmx中查詢數據並返回List。然後,我將列表綁定到BindingSource並將其作爲DataSource for DataGridView添加。但在應用程序執行時,當我嘗試向DataGridView添加新行時,我得到以下異常:「System.ArgumentException:DataGridViewComboBoxCell值無效」 我添加了bindingSource.AllowNew = true,但仍缺少一些內容。 你能告訴我我做錯了什麼或失蹤嗎?如何將新行添加到綁定到List的DataGridView中<MyTableClass>

我想要做的就是讓可更新的DataGridView使用WCF服務。

編輯:

服務方法:

public List<Cities> GetCities() 
{ 
    try 
    { 
     CasinoEntities db = new CasinoEntities(); 
     List<Cities> lsCities = (from e in db.Cities select e).ToList<Cities>(); 
     return lsCities; 
    } 
    catch (Exception) 
    { 
     throw; 
    } 
    return null; 
} 

還有就是如何獲取列表,並將其綁定:

private void Form_Load(object sender, EventArgs e) 
{ 
    Service1Client dataServiceClient = new Service1Client(); 
    List<Cities> citiesList = dataServiceClient.GetCities().ToList(); 

    this.citiesBindingSource.AllowNew = true; 
    this.citiesBindingSource.DataSource = citiesList; 
    this.dgvData.DataSource = this.citiesBindingSource; 
} 

沒有從我的實體模型生成的城市類:

[Serializable()] 
[DataContractAttribute(IsReference=true)] 
public partial class Cities : EntityObject 
{ 
    public static Cities CreateCities(global::System.Int32 id, global::System.String name, global::System.Int32 countryId, global::System.Byte[] timestamp) 
    { 
     Cities cities = new Cities(); 
     cities.Id = id; 
     cities.Name = name; 
     cities.CountryId = countryId; 
     cities.Timestamp = timestamp; 
     return cities; 
    } 

    [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)] 
    [DataMemberAttribute()] 
    public global::System.Int32 Id 
    { 
     get 
     { 
      return _Id; 
     } 
     set 
     { 
      if (_Id != value) 
      { 
       OnIdChanging(value); 
       ReportPropertyChanging("Id"); 
       _Id = StructuralObject.SetValidValue(value); 
       ReportPropertyChanged("Id"); 
       OnIdChanged(); 
      } 
     } 
    } 
    private global::System.Int32 _Id; 
    partial void OnIdChanging(global::System.Int32 value); 
    partial void OnIdChanged(); 

    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] 
    [DataMemberAttribute()] 
    public global::System.String Name 
    { 
     get 
     { 
      return _Name; 
     } 
     set 
     { 
      OnNameChanging(value); 
      ReportPropertyChanging("Name"); 
      _Name = StructuralObject.SetValidValue(value, false); 
      ReportPropertyChanged("Name"); 
      OnNameChanged(); 
     } 
    } 
    private global::System.String _Name; 
    partial void OnNameChanging(global::System.String value); 
    partial void OnNameChanged(); 

    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] 
    [DataMemberAttribute()] 
    public global::System.Int32 CountryId 
    { 
     get 
     { 
      return _CountryId; 
     } 
     set 
     { 
      OnCountryIdChanging(value); 
      ReportPropertyChanging("CountryId"); 
      _CountryId = StructuralObject.SetValidValue(value); 
      ReportPropertyChanged("CountryId"); 
      OnCountryIdChanged(); 
     } 
    } 
    private global::System.Int32 _CountryId; 
    partial void OnCountryIdChanging(global::System.Int32 value); 
    partial void OnCountryIdChanged(); 

    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] 
    [DataMemberAttribute()] 
    public global::System.Byte[] Timestamp 
    { 
     get 
     { 
      return StructuralObject.GetValidValue(_Timestamp); 
     } 
     set 
     { 
      OnTimestampChanging(value); 
      ReportPropertyChanging("Timestamp"); 
      _Timestamp = StructuralObject.SetValidValue(value, true); 
      ReportPropertyChanged("Timestamp"); 
      OnTimestampChanged(); 
     } 
    } 
    private global::System.Byte[] _Timestamp; 
    partial void OnTimestampChanging(global::System.Byte[] value); 
    partial void OnTimestampChanged(); 
} 
+0

哪裏是你的代碼 – 2012-02-24 16:21:16

回答

3

當您嘗試添加一個新的記錄bindingSource將嘗試創建的 Cities一個實例,所以我的猜測是,你有countryId屬性,它採用上創建的Cities新實例0的組合框柱,讓我們說你初始化comboColCountryList,解決的辦法是:

CountryList.Insert(0,new CountryClass() {CountryId = 0, CountryName = "<Select country>"}); 
+0

大男人,非常感謝 – Lukas 2012-02-27 08:50:53

+0

謝謝,你有我的一票:) – 2012-02-27 09:07:16

相關問題