以编程方式设置C#Winforms时,DataGridViewComboxCell值不更新
我已经阅读了所有关于这个该死的控件(DataGridViewComboxCell)的类似文章,并以编程方式设置了它的值,但是实现所有的建议都不起作用。我可能会改变用户界面来解决这个问题,但我不喜欢被打败以编程方式设置C#Winforms时,DataGridViewComboxCell值不更新,winforms,datagridviewcomboboxcell,Winforms,Datagridviewcomboboxcell,我已经阅读了所有关于这个该死的控件(DataGridViewComboxCell)的类似文章,并以编程方式设置了它的值,但是实现所有的建议都不起作用。我可能会改变用户界面来解决这个问题,但我不喜欢被打败 private void PopulateAreaForRoleAssociation() { // If businessRoleList is null then no data has been bound to the dgv so return
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);
}
}
}
});
}
private void populateareaforroleasociation()
{
//如果businessRoleList为null,则没有数据绑定到dgv so返回
if(businessRoleList==null)
返回;
//确保已实例化businessArea存储库
如果(businessAreaRepository==null)
businessAreaRepository=newBusinessAreaRespository();
//获取对dgv的combobox列的引用
DataGridViewComboBoxColumn ComboxBusinessAreaColumn=(DataGridViewComboxColumn)dgvBusinessRole.Columns[“BusinessArea”];
//设置数据源属性以填充组合框
comboBoxBusinessAreaColumn.DisplayMember=“Name”;
comboBoxBusinessAreaColumn.ValueMember=“Id”;
comboBoxBusinessAreaColumn.ValueType=typeof(Guid);
//用存储库列表中的businessarea对象填充组合框
comboBoxBusinessAreaColumn.DataSource=businessAreaRepository.GetAll();
//循环dgv绑定到的businessRoles列表,并根据循环中的当前id取出每个dgv行
businessRoleList.Cast().ToList().ForEach(委托(BusinessRole currentRole)
{
DataGridViewRow currentRowForRole=dgvBusinessRole.Rows.Cast().ToList().Find(row=>((BusinessRole)row.DataBoundItem.Id==currentRole.Id);
//获取对当前行中comboBox单元格的引用
DataGridViewComboxCell ComboxCell=(DataGridViewComboxCell)currentRowForRole.Cells[2];
//不确定这是否必要,因为这些属性应该从combobox列属性继承
comboBoxCell.DisplayMember=“Name”;
comboBoxCell.ValueMember=“Id”;
comboBoxCell.ValueType=typeof(Guid);
//获取当前业务角色的业务领域
BusinessArea currentAreaForRole=businessAreaRepository.FetchByRoleId(currentRole.Id);
//如果角色具有关联区域,则将单元格的值设置为组合框中的相应项
//并更新单元格值
if(currentAreaForRole!=null)
{
foreach(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实体相关联。如果是,那么我想在combobox列中选择保存BusinessAreas的项目
问题是,当加载网格时,所有数据都在那里,组合框中填充了可用区域的列表,但是没有显示任何设置的值。设置值的代码肯定会被命中,而我设置的值肯定存在于列表中
没有数据错误,什么都没有。它只是拒绝用我编程设置的值更新UI
我们将一如既往地感谢您的帮助
谢谢这段代码适用于我所在行的第一个组合框,其中包含以下代码,但我在该行的下一个组合框中尝试了这段代码,但它不起作用。剩下的我还有3件事要做。我确实使用与try块最后一行相同的代码在该表单的第二个表单上设置组合框,但使用单元格信息而不是数据集
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)
{
}
我也有同样的问题!我会继续尝试和阅读,如果我发现了什么,我会把它贴在这里!