我已经阅读了关于这个该死的控件(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对象的绑定列表,然后combobox列被绑定到来自存储库类的BusinessArea对象的基本列表。然后,我循环遍历bindinglist,并拉出绑定到bindinglist循环中当前项的dgv行。对于该行,我进行数据库调用,查看BusinessRole实体是否与BusinessArea实体相关联。如果是,那么我想在包含BusinessArea的组合框列中选择项目。
问题是,当加载网格时,所有数据都在那里,并且组合框中填充了可用区域的列表,但设置的任何值都不会显示。设置值的代码肯定会被命中,而我正在设置的值肯定存在于列表中。
没有数据错误,什么都没有。它只是拒绝使用我通过编程设置的值更新UI。
如有任何帮助,我们将一如既往地不胜感激。
感谢
这段代码适用于我所在行中的第一个组合框和下面的代码,但我尝试使用该行中的下一个组合框,但它不起作用。我可以使用其他的帮助,我还有3个要做。我确实在这个表单的第二个表单上设置了组合框,代码与尝试块的最后一行相同,但使用的是单元格信息,而不是数据集
try
{
string strQuery = "select fundCode from vwDefaultItems where IncomeType = '" + stIncome + "'";
SqlDataAdapter daDefault = new SqlDataAdapter(strQuery, conn);
DataSet dsDefault = new DataSet();
daDefault.Fill(dsDefault);
strDefFund = dsDefault.Tables[0].Rows[0].ItemArray[0].ToString();
dgvCheckEntry.Rows[curRow].Cells[7].Value = dsDefault.Tables[0].Rows[0].ItemArray[0].ToString();
}
catch (Exception eq)
{
}