2013-02-09 46 views
0

我读过关于此darn控件(DataGridViewComboBoxCell)的所有相似的帖子,并以编程方式设置它的值,但实现所有的建议没有奏效。我可以改变UI来解决这个问题,但我不喜欢被打败!DataGridViewComboBoxCell价值不更新时编程设置C#Winforms

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对象的基本列表。然后,我通过绑定列表循环,并取出绑定到绑定列表循环中当前项目的dgv行。 使用该行,我进行数据库调用以查看BusinessRole实体是否与BusinessArea实体关联。如果是,那么我想选择包含BusinessAreas的组合框列中的项目。

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

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

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

感谢

+0

我有此相同问题的数据集!我会继续尝试和周围阅读,如果我找到任何东西,我会在这里发布! – 2013-02-20 19:06:45

回答

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

     }