我有一個多到一些與主要細節DataGrid設置混合。DataGridViewComboBoxColumn沒有采取ValueMember
- 4的DataGridView
- 6實體(2映射表)
- 5數據關係
爲了給你的視覺圖像,有1大僱工DGV(DataGridView中),和3小根據僱員DGV選定行進行更新。
3個較小的網格之一是一個非常簡單的沒有映射表的關係。
然而,另外2個顯示映射表的行使用formatedinfo它正在映射到的實體。這兩個DGV只有一列,一個帶有格式化字符串的組合框(覆蓋.ToString()
),它應該具有映射指向的基礎對象(EF將在接收整個對象時相應地設置ID)。但是,當我嘗試添加新行時,它會說它無法將字符串轉換爲將對象名稱放在這裏。這導致我認爲如果你沒有設置ValueMember,那麼組合框將嘗試使用.ToString()來獲取字符串表示。所以我試圖繞過這種行爲,通過將Self
屬性添加到只返回自己的實體。沒有成功,它仍然試圖在某處轉換字符串。
確切的錯誤(法語)是:
System.FormatException: Cast non valide de 'System.String' en 'InvInformatique.logiciels'.
從System.String要InvInformatique.logiciels traducts爲「無效的轉換
代碼
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"));
。
數據庫
個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
數據庫關係
Employes <-> Logiciel_Employe_Maps <-> Logiciels
Employes <-> Acces_Employe_Maps <-> Acces
Employes -> Materiel