如何将新行添加到绑定到List<;的DataGridView;MyTableClass>;
本文关键字:lt DataGridView gt MyTableClass List 新行 添加 绑定 | 更新日期: 2023-09-27 18:25:54
我有一个WCF服务,它从生成的实体数据模型edmx中查询数据并返回List。然后,我将列表绑定到BindingSource,并将其添加为DataGridView的DataSource。但在应用程序执行时,当我尝试向DataGridView添加新行时,我会得到异常:"System.ArgumentException:DataGridViewComboBoxCell值无效"我添加了bindingSource.AllowNew=true,但仍然缺少一些内容。你能告诉我我做错了什么或错过了什么吗?
我所想做的就是使用WCF服务拥有可更新的DataGridView。
编辑:
服务方式:
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;
}
有一个从我的实体模型生成的Cities类:
[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();
}
当您尝试添加新记录时,bindingSource
将尝试创建Cities
,所以我猜你在countryId
属性上有一个combobox列,它在创建Cities
的新实例时取0,所以假设你用CountryList
初始化comboCol
,解决方案是:
CountryList.Insert(0,new CountryClass() {CountryId = 0, CountryName = "<Select country>"});