以编程方式设置C#Winforms时,DataGridViewComboBoxCell值不更新

时间:2013-02-09 13:54:18

标签: winforms datagridviewcomboboxcell

我已经阅读了有关此darn控件(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循环中当前项的dgv行。 使用该行,我进行数据库调用以查看BusinessRole实体是否与BusinessArea实体相关联。如果是,那么我想选择包含BusinessAreas的组合框列中的项目。

问题在于,当加载网格时,所有数据都在那里,并且组合框中填充了可用区域列表,但是不显示任何设置的值。设置值的代码肯定会被命中,而我设置的值肯定会出现在列表中。

没有数据错误,没有。它只是拒绝使用我以编程方式设置的值更新UI。

任何帮助都将一如既往地受到高度赞赏。

由于

1 个答案:

答案 0 :(得分:0)

此代码适用于我在我的行中使用以下代码的第一个组合框,但我尝试使用行中的下一个组合框,但它不起作用。我可以使用其余的帮助我还有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)
      {

      }