0

我已经阅读了所有关于这个该死的控件(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。

任何帮助将一如既往地不胜感激。

谢谢

4

1 回答 1

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)
      {

      } 
于 2013-03-01T15:22:07.093 回答