/// <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> /// Creates a new ComboBoxConfiguration with a single itemSource for all combobox cell of a combobox column /// </summary> /// <param name="srcColumnName">the name of the column that the combobox column is bounded to</param> /// <param name="itemListSource">the ItemSource for the ItemList of all combobox cells</param> /// <param name="sortItemList">sort the itemLists?</param> /// <returns>configation for a comboBoxColumn</returns> private ComboBoxConfiguration GetComboBoxConfigFromSingleDataSource(BindingList <object> itemListSource, ComboboxConfigType configurationType, bool sortItemList) { Dictionary <object, BindingList <object> > dataSourceDictionary = new Dictionary <object, BindingList <object> >(); foreach (DataGridViewRow row in this.Rows) { dataSourceDictionary.Add(row.DataBoundItem, itemListSource); } //only one registration is necessary because all datasources are equal itemListSource.ListChanged += DataSource_ListChanged; //Create combobox configuration and add item source template for adding new columns ComboBoxConfiguration comboConfig = new ComboBoxConfiguration(dataSourceDictionary, configurationType, sortItemList); comboConfig.AddItenSourceTemplate(itemListSource); return(comboConfig); }
/// <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; }
/// <summary> /// Sets values and itemList for all combobox cells for a bounded column spefified by the parameter columnName /// </summary> /// <param name="columnName">column name of the bounde column</param> public void RefreshCellBoundComboBox(string columnName) { IsCellValueChangeEventDisabled = true; int columnIndex = this.Columns[CMB_COLUMN_PREFIX + columnName].Index; int boundedColumnIndex = GetBoundedColumnIndex(columnIndex); ComboBoxConfiguration comboConfig = _cmbItemSources[columnIndex]; foreach (DataGridViewRow row in this.Rows) { DataGridViewCell boundedCell = row.Cells[boundedColumnIndex]; try { List <object> itemList = comboConfig.GetItemList(row.DataBoundItem); object value = boundedCell.Value; //null values are not allowed if (value == null) { boundedCell.Value = ""; value = ""; } if (!itemList.Contains(value)) { itemList.Insert(0, value); } DataGridViewComboBoxCell comboboxCell = ((DataGridViewComboBoxCell)row.Cells[columnIndex]); SetItemList(comboboxCell, itemList); comboboxCell.Value = value; } catch (Exception) { } } IsCellValueChangeEventDisabled = false; }
// <summary> /// Adds a combobox column that is bounded to another Column (bounded column) /// </summary> /// <param name="srcColumnName">the name of the column that the combobox column is bounded to</param> /// <param name="comboConfig">the configuration of the combobox cell</param> private void AddCellBoundedComboBox(string srcColumnName, ComboBoxConfiguration comboConfig) { IsCellValueChangeEventDisabled = true; DataGridViewComboBoxColumn cmbColumn = new DataGridViewComboBoxColumn(); DataGridViewColumn srcColumn = this.Columns[srcColumnName]; srcColumn.Visible = false; cmbColumn.Name = CMB_COLUMN_PREFIX + srcColumnName; cmbColumn.HeaderText = srcColumn.HeaderText; cmbColumn.ValueType = srcColumn.ValueType; cmbColumn.FlatStyle = FlatStyle.Flat; int index = _cmbItemSources.Count; _cmbItemSources.Add(index, comboConfig); this.Columns.Insert(index, cmbColumn); this.Columns[index].DisplayIndex = srcColumn.Index; //Copy Values from the bounded cell (bounded to grid datasource) to the combobox cell foreach (DataGridViewRow row in this.Rows) { List <object> itemList = comboConfig.GetItemList(row.DataBoundItem); object value = row.Cells[srcColumn.Index].Value; if (!itemList.Contains(value)) { itemList.Insert(0, value); } DataGridViewComboBoxCell cell = ((DataGridViewComboBoxCell)row.Cells[cmbColumn.Index]); SetItemList(cell, itemList); row.Cells[cmbColumn.Index].Value = row.Cells[srcColumn.Index].Value; } IsCellValueChangeEventDisabled = false; }
/// <summary> /// Adds a combobox column that is bounded to another Column (bounded column) /// (all combobox cells have different itemSources /// </summary> /// <param name="srcColumnName">the name of the column that the combobox column is bounded to</param> /// <param name="itemListSource">the ItemSources for the ItemLists of the combobox cells</param> /// <param name="configurationType">configuration type</param> /// <param name="sortItemList">sort the itemLists?</param> public void AddCellBoundedComboBox(string srcColumnName, Dictionary <object, BindingList <object> > itemListSource, ComboboxConfigType configurationType, bool sortItemList) { ComboBoxConfiguration comboConfig = new ComboBoxConfiguration(itemListSource, configurationType, sortItemList); AddCellBoundedComboBox(srcColumnName, comboConfig); }
/// <summary> /// Adds a combobox column that is bounded to another Column (bounded column) /// (all combobox cells have different itemSources /// </summary> /// <param name="srcColumnName">the name of the column that the combobox column is bounded to</param> /// <param name="itemListSource">the ItemSources for the ItemLists of the combobox cells</param> /// <param name="configurationType">configuration type</param> /// <param name="sortItemList">sort the itemLists?</param> public void AddCellBoundedComboBox(string srcColumnName, BindingList <object> itemListSource, ComboboxConfigType configurationType, bool sortItemList) { ComboBoxConfiguration comboConfig = GetComboBoxConfigFromSingleDataSource(itemListSource, configurationType, sortItemList); AddCellBoundedComboBox(srcColumnName, comboConfig); }