/// <summary> /// If a cell value has changed /// 1. the ItemList has to be updated (remove or add invalid item) /// 2. the bounded cell has to be updated /// 3. the fore color of the combobox value has to be updated /// </summary> /// <param name="e">event arguments</param> private void DoCellValueChanged(DataGridViewCellEventArgs e) { IsCellValueChangeEventDisabled = true; if (_cmbItemSources.Keys.Contains(e.ColumnIndex)) { DataGridViewRow row = this.Rows[e.RowIndex]; ComboBoxConfiguration comboConfig = _cmbItemSources[e.ColumnIndex]; //Get the new value of the cell (if value has been choosen by a an EdintingControl like a combobox, the cell still contains the old value) object value = (EditingControl != null ? EditingControl.Text : this.Rows[e.RowIndex].Cells[e.ColumnIndex].Value); if (value == null) { value = ""; } //Update combobox Itemlist List <object> itemList = comboConfig.GetItemList(row.DataBoundItem); if (!itemList.Contains(value)) { itemList.Insert(0, value); } DataGridViewComboBoxCell cell = ((DataGridViewComboBoxCell)row.Cells[e.ColumnIndex]); SetItemList(cell, itemList); //Update bounded Cell (it is databounded to the GridViews DataSource) row.Cells[GetBoundedColumnIndex(e.ColumnIndex)].Value = value; //Update ForeColor of ComboBoxCell row.Cells[e.ColumnIndex].Style.ForeColor = comboConfig.GetForeColor(value, row.DataBoundItem); } IsCellValueChangeEventDisabled = false; }
/// <summary> /// React if the ItemSource for the ItemList has changed /// </summary> /// <param name="sender">event sender</param> /// <param name="e">event arguments</param> private void DataSource_ListChanged(object sender, ListChangedEventArgs e) { IsCellValueChangeEventDisabled = true; BindingList <object> itemSource = (BindingList <object>)sender; // Iterate through all combobox configurations to find the config that contains the changed itemSource foreach (int columnIndex in _cmbItemSources.Keys) { ComboBoxConfiguration comboConfig = _cmbItemSources[columnIndex]; if (comboConfig.HasItemSource(itemSource)) { foreach (DataGridViewRow row in this.Rows) { if (comboConfig.HasItemSource(itemSource, row.DataBoundItem)) { List <object> itemList = comboConfig.GetItemList(row.DataBoundItem); object value = row.Cells[columnIndex].Value; DataGridViewComboBoxCell cell = ((DataGridViewComboBoxCell)row.Cells[columnIndex]); //the cells value must be containced in the itemList: if (!itemList.Contains(value)) { itemList.Insert(0, value); } //Update ItemList SetItemList(cell, itemList); //Update Fore Color of the combobox cell row.Cells[columnIndex].Style.ForeColor = comboConfig.GetForeColor(value, row.DataBoundItem); } } } } IsCellValueChangeEventDisabled = false; }