2012-02-24 65 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

相关问题