このダーン コントロール (DataGridViewComboBoxCell) に関する同様の投稿をすべて読み、その値をプログラムで設定しましたが、すべての提案を実装してもうまくいきませんでした。この問題を回避するために UI を変更することもできますが、負けたくありません。
private void PopulateAreaForRoleAssociation()
{
// If businessRoleList is null then no data has been bound to the dgv so return
if (businessRoleList == null)
return;
// Ensure businessArea repository is instantiated
if (businessAreaRepository == null)
businessAreaRepository = new BusinessAreaRespository();
// Get a reference to the combobox column of the dgv
DataGridViewComboBoxColumn comboBoxBusinessAreaColumn = (DataGridViewComboBoxColumn)dgvBusinessRole.Columns["BusinessArea"];
// Set Datasource properties to fill combobox
comboBoxBusinessAreaColumn.DisplayMember = "Name";
comboBoxBusinessAreaColumn.ValueMember = "Id";
comboBoxBusinessAreaColumn.ValueType = typeof(Guid);
// Fill combobox with businessarea objects from list out of repository
comboBoxBusinessAreaColumn.DataSource = businessAreaRepository.GetAll();
// loop through the businessRoles list which the dgv is bound to and get out each dgv row based upon the current id in the loop
businessRoleList.Cast<BusinessRole>().ToList().ForEach(delegate(BusinessRole currentRole)
{
DataGridViewRow currentRowForRole = dgvBusinessRole.Rows.Cast<DataGridViewRow>().ToList().Find(row => ((BusinessRole)row.DataBoundItem).Id == currentRole.Id);
// Get a reference to the comboBox cell in the current row
DataGridViewComboBoxCell comboBoxCell = (DataGridViewComboBoxCell)currentRowForRole.Cells[2];
// Not sure if this is necessary since these properties should be inherited from the combobox column properties
comboBoxCell.DisplayMember = "Name";
comboBoxCell.ValueMember = "Id";
comboBoxCell.ValueType = typeof(Guid);
// Get the business area for the current business role
BusinessArea currentAreaForRole = businessAreaRepository.FetchByRoleId(currentRole.Id);
// if the role has an associated area then set the value of the cell to be the appropriate item in the combobox
// and update the cell value
if (currentAreaForRole != null)
{
foreach (BusinessArea area in comboBoxCell.Items)
{
if (currentAreaForRole.Id == area.Id)
{
comboBoxCell.Value = area.Id;
dgvBusinessRole.UpdateCellValue(2, comboBoxCell.RowIndex);
}
}
}
});
}
dgv は最初に BusinessRole オブジェクトを保持するバインディング リストにバインドされ、次にコンボボックス列がリポジトリ クラスから生成される BusinessArea オブジェクトの基本リストにバインドされます。次に、bindinglist をループして、bindinglist ループ内の現在のアイテムにバインドされている dgv の行を取り出します。その行を使用してデータベース呼び出しを行い、BusinessRole エンティティが BusinessArea エンティティに関連付けられているかどうかを確認します。そうであれば、BusinessAreas を保持するコンボボックス列の項目を選択したいと思います。
問題は、グリッドがロードされると、すべてのデータがそこにあり、コンボボックスに使用可能な領域のリストが取り込まれますが、設定されている値が表示されないことです。値を設定するコードは間違いなくヒットし、設定している値は間違いなくリストに存在します。
データエラーはありません。プログラムで設定した値で UI を更新することを拒否しているだけです。
いつものようにどんな助けでも大歓迎です。
ありがとう