DataGridViewComboBoxColumn没有使用ValueMember [英] DataGridViewComboBoxColumn not taking ValueMember

查看:235
本文介绍了DataGridViewComboBoxColumn没有使用ValueMember的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




  • 4 DataGridView

  • 6个实体(2个映射表)

  • 5数据关系



给你一个视觉图像,有1个大的雇主DGV(DataGridView),3个较小的根据雇主DGV选择的行更新。



3个较小的网格之一是一个非常简单的关系,没有映射表。



然而,另外2个显示使用其映射到的实体的formatedinfo的映射表行。那两个DGV只有1列,一个ComboBox与一个格式的字符串(覆盖 .ToString()),(应该)有映射所指向的基础对象(EF将在接收到整个对象时相应地处理ID的设置)。但是,当我尝试添加一个新行时,它会说它无法将一个String转换为将对象名称放在这里。这导致我认为如果您没有设置ValueMember,组合框将尝试使用.ToString()获取字符串表示。所以我试图通过向简单地返回自己的实体添加一个 Self 属性来绕过这种行为。没有成功,它仍然尝试将字符串转换到某处。



确切的错误(法语)是:



System.FormatException:将无法更改的'System.String'en'InvInformatique.logiciels'。



哪些传递到System.String到InvInformatique.logiciels的无效转换。



代码



i

bsEmployes.DataSource = iidb.employes;
bsMateriels.DataSource = iidb.materiels;
bsLogiciels.DataSource = iidb.logiciels;
bsLogicielEmployeMaps.DataSource = iidb.logiciel_employe_maps; //需要引发加载查询
bsAcces.DataSource = iidb.acces;
bsAccesEmployeMaps.DataSource = iidb.acces_employe_maps; //需要引发加载查询

dgvEmployes.AutoGenerateColumns = false;
dgvEmployes.Columns.AddRange(
new DataGridViewTextBoxColumn {Name = Nom,DataPropertyName =nom},
new DataGridViewTextBoxColumn {Name =Département,DataPropertyName =departement}
);
dgvEmployeMateriels.AutoGenerateColumns = false;
dgvEmployeMateriels.Columns.AddRange(
new DataGridViewTextBoxColumn {HeaderText =Nom,DataPropertyName =nom},
new DataGridViewTextBoxColumn {HeaderText =Satisfaction,Name =satisfaction,DataPropertyName =满意}
);
dgvEmployeAcces.AutoGenerateColumns = false;
dgvEmployeAcces.Columns.AddRange(
new DataGridViewComboBoxColumn {HeaderText =Accès,DataPropertyName =acces,DataSource = bsAcces}
);
dgvEmployeLogiciel.AutoGenerateColumns = false;
dgvEmployeLogiciel.Columns.AddRange(
new DataGridViewComboBoxColumn {HeaderText =Logiciels,DataPropertyName =logiciels,DataSource = bsLogiciels}
);

dgvEmployes.DataSource = bsEmployes;
dgvEmployeMateriels.DataBindings.Add(new Binding(DataSource,bsEmployes,materiels));
dgvEmployeLogiciel.DataBindings.Add(new Binding(DataSource,bsEmployes,logiciel_employe_maps));
dgvEmployeAcces.DataBindings.Add(new Binding(DataSource,bsEmployes,acces_employe_maps));

数据库

 雇用
- > id
- > nom
- >离开

加入
- > id
- > nom
- > description

Logiciel
- > id
- > nom

Materiel
- > id
- > employe_id
- > nom
- > description

Acces_Employe_Maps
- > ; id
- > acces_id
- > employe_id

Logiciel_Employe_Maps
- > id
- > logiciel_id
- > employe_id

数据库关系

 雇用<  - > Logiciel_Employe_Maps<  - > Logiciels 
雇用< - > Acces_Employe_Maps< - > Acces
雇用 - >物品


解决方案

好的,DataGridViews不喜欢使用ToString ()获取一个DisplayMember。当我保持DisplayMember为空或使用我的自我属性时,它会将ValueMember错误。



设置 DisplayMember =nom,ValueMember =Self 完美工作。



修改



工作:



添加到EF部分类:

  public string FormatedName {get {return this.ToString(); }} 

更改列定义:

  DisplayMember =FormatedName,ValueMember =Self


I have a Many to Many mixed with some Master-Detail DataGrid setup.

  • 4 DataGridView
  • 6 Entities (2 mapping tables)
  • 5 Data relations

To give you a visual image, there is 1 big Employe DGV (DataGridView), and 3 smaller ones which updates depending on the Employe DGV selected row.

One of the 3 smaller grid is a really simple relation without a mapping table.

The other 2, however, displays a mapping table rows that uses formatedinfo of the entity it is mapping to. Those 2 DGV only have 1 column, a ComboBox with a formated string (overriden .ToString()) which (should) have the underlying object that the mapping is pointing to (EF will handle setting the ID accordingly when receiving the entire object). However, when I try to add a new rows, it will say it is unable to convert a String to a Put Object Name Here. This led me to think that if you did not set a ValueMember, the combobox would try to get a string representation using .ToString(). So I attempted to bypass that behavior by adding a Self property to the entity which simply returns himself. No success, it is still trying to convert a string somewhere.

The exact error (in french) is:

System.FormatException: Cast non valide de 'System.String' en 'InvInformatique.logiciels'.

Which traducts to "Invalid cast from System.String to InvInformatique.logiciels.

Code

InventaireInformatiqueEntities iidb = new InventaireInformatiqueEntities();

bsEmployes.DataSource = iidb.employes;
bsMateriels.DataSource = iidb.materiels;
bsLogiciels.DataSource = iidb.logiciels;
bsLogicielEmployeMaps.DataSource = iidb.logiciel_employe_maps; // Needed to provoke the load query
bsAcces.DataSource = iidb.acces;
bsAccesEmployeMaps.DataSource = iidb.acces_employe_maps; // Needed to provoke the load query

dgvEmployes.AutoGenerateColumns = false;
dgvEmployes.Columns.AddRange(
    new DataGridViewTextBoxColumn { Name = "Nom", DataPropertyName = "nom" },
    new DataGridViewTextBoxColumn { Name = "Département", DataPropertyName = "departement" }
);
dgvEmployeMateriels.AutoGenerateColumns = false;
dgvEmployeMateriels.Columns.AddRange(
    new DataGridViewTextBoxColumn { HeaderText = "Nom", DataPropertyName = "nom" },
    new DataGridViewTextBoxColumn { HeaderText = "Satisfaction", Name = "satisfaction", DataPropertyName = "satisfaction" }
);
dgvEmployeAcces.AutoGenerateColumns = false;
dgvEmployeAcces.Columns.AddRange(
    new DataGridViewComboBoxColumn { HeaderText = "Accès", DataPropertyName = "acces", DataSource = bsAcces }
);
dgvEmployeLogiciel.AutoGenerateColumns = false;
dgvEmployeLogiciel.Columns.AddRange(
    new DataGridViewComboBoxColumn { HeaderText = "Logiciels", DataPropertyName = "logiciels", DataSource = bsLogiciels }
);

dgvEmployes.DataSource = bsEmployes;
dgvEmployeMateriels.DataBindings.Add(new Binding("DataSource", bsEmployes, "materiels"));
dgvEmployeLogiciel.DataBindings.Add(new Binding("DataSource", bsEmployes, "logiciel_employe_maps"));
dgvEmployeAcces.DataBindings.Add(new Binding("DataSource", bsEmployes, "acces_employe_maps"));

Database

Employes
->id
->nom
->departement

Acces
->id
->nom
->description

Logiciel
->id
->nom

Materiel
->id
->employe_id
->nom
->description

Acces_Employe_Maps
->id
->acces_id
->employe_id

Logiciel_Employe_Maps
->id
->logiciel_id
->employe_id

Database relations

Employes <-> Logiciel_Employe_Maps <-> Logiciels
Employes <-> Acces_Employe_Maps <-> Acces
Employes -> Materiel

解决方案

Ok so, DataGridViews don't like using ToString() to get a DisplayMember. When I keep DisplayMember empty or use my Self property, it bugs out the ValueMember.

Setting DisplayMember = "nom", ValueMember="Self" worked perfectly.

Edit

This also worked:

Add to EF partial class:

public string FormatedName { get { return this.ToString(); } }

Change in column definition:

DisplayMember = "FormatedName", ValueMember = "Self"

这篇关于DataGridViewComboBoxColumn没有使用ValueMember的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆