DataGridViewComboEditBoxCell.cs
资源名称:CombEdit.rar [点击查看]
上传用户:b2s168
上传日期:2021-04-20
资源大小:45k
文件大小:3k
源码类别:
组合框控件
开发平台:
Visual C++
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows.Forms;
- using System.ComponentModel;
- namespace CombEdit
- {
- public class DataGridViewComboEditBoxCell : DataGridViewComboBoxCell
- {
- public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
- {
- base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
- ComboBox comboBox = base.DataGridView.EditingControl as ComboBox;
- if (comboBox != null)
- {
- comboBox.DropDownStyle = ComboBoxStyle.DropDown;
- comboBox.Validating += new CancelEventHandler(comboBox_Validating);
- }
- }
- protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
- {
- if (value != null && DataSource==null)
- {
- if (value.ToString().Trim() != string.Empty)
- {
- if (Items.IndexOf(value) == -1)
- {
- Items.Add(value);
- DataGridViewComboBoxColumn col = OwningColumn as DataGridViewComboBoxColumn;
- col.Items.Add(value);
- }
- }
- }
- return base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);
- }
- void comboBox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
- {
- DataGridViewComboBoxEditingControl cbo = sender as DataGridViewComboBoxEditingControl;
- if (cbo.Text.Trim() == string.Empty) return;
- DataGridView grid = cbo.EditingControlDataGridView;
- object value = cbo.Text;
- // Add value to list if not there
- if (cbo.Items.IndexOf(value) == -1)
- {
- DataGridViewComboBoxColumn cboCol = grid.Columns[grid.CurrentCell.ColumnIndex] as DataGridViewComboBoxColumn;
- // Must add to both the current combobox as well as the template, to avoid duplicate entries
- if (DataSource == null)
- {
- cbo.Items.Add(value);
- cboCol.Items.Add(value);
- grid.CurrentCell.Value = value;
- }
- }
- }
- }
- public class DataGridViewComboEditBoxColumn : DataGridViewComboBoxColumn
- {
- public DataGridViewComboEditBoxColumn()
- {
- DataGridViewComboEditBoxCell obj = new DataGridViewComboEditBoxCell();
- this.CellTemplate = obj;
- }
- }
- }