DataGridViewComboBoxExEditingControl.cs
资源名称:CombEdit.rar [点击查看]
上传用户:b2s168
上传日期:2021-04-20
资源大小:45k
文件大小:6k
源码类别:
组合框控件
开发平台:
Visual C++
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows.Forms;
- namespace CombEdit
- {
- //设计一个继承自ComboBox的下拉框编辑列组件
- public class DataGridViewComboBoxExEditingControl : ComboBox, IDataGridViewEditingControl
- {
- protected int rowIndex;
- protected DataGridView dataGridView;
- protected bool valueChanged = false;
- protected override void OnTextChanged(System.EventArgs e)
- {
- base.OnTextChanged(e);
- NotifyDataGridViewOfValueChange();
- }
- private void NotifyDataGridViewOfValueChange()
- {
- valueChanged = true;
- dataGridView.NotifyCurrentCellDirty(true);
- }
- protected override void OnSelectedIndexChanged(EventArgs e)
- {
- base.OnTextChanged(e);
- NotifyDataGridViewOfValueChange();
- }
- public Cursor EditingPanelCursor
- {
- get { return Cursors.IBeam; }
- }
- public DataGridView EditingControlDataGridView
- {
- get { return dataGridView; }
- set { dataGridView = value; }
- }
- public object EditingControlFormattedValue
- {
- set
- {
- Text = value.ToString();
- NotifyDataGridViewOfValueChange();
- }
- get
- {
- return this.Text;
- }
- }
- public virtual object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
- {
- return Text;
- }
- public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
- {
- switch (key & Keys.KeyCode)
- {
- case Keys.Left:
- case Keys.Up:
- case Keys.Down:
- case Keys.Right:
- case Keys.Home:
- case Keys.End:
- case Keys.Escape:
- case Keys.Enter:
- case Keys.PageDown:
- case Keys.PageUp:
- return true;
- default:
- return false;
- }
- }
- public void PrepareEditingControlForEdit(bool selectAll)
- {
- if (selectAll)
- {
- SelectAll();
- }
- else
- {
- this.SelectionStart = this.ToString().Length;
- }
- }
- public virtual bool RepositionEditingControlOnValueChange
- {
- get
- {
- return false;
- }
- }
- public int EditingControlRowIndex
- {
- get { return this.rowIndex; }
- set { this.rowIndex = value; }
- }
- public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
- {
- this.Font = dataGridViewCellStyle.Font;
- this.ForeColor = dataGridViewCellStyle.ForeColor;
- this.BackColor = dataGridViewCellStyle.BackColor;
- }
- public bool EditingControlValueChanged
- {
- get { return valueChanged; }
- set { this.valueChanged = value; }
- }
- }
- //定制该扩展列的单元格
- public class DataGridViewComboBoxExCell : DataGridViewTextBoxCell
- {
- public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
- {
- base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
- DataGridViewComboBoxExEditingControl clt = DataGridView.EditingControl as DataGridViewComboBoxExEditingControl;
- DataGridViewComboBoxExColumn col = (DataGridViewComboBoxExColumn)OwningColumn;
- clt.DataSource = col.DataSource;
- clt.DisplayMember = col.DisplayMember;
- clt.ValueMember = col.ValueMember;
- clt.Text = Convert.ToString(this.Value);
- }
- public override Type EditType
- {
- get
- {
- return typeof(DataGridViewComboBoxExEditingControl);
- }
- }
- public override Type ValueType
- {
- get
- {
- return typeof(string);
- }
- }
- public override object DefaultNewRowValue
- {
- get
- {
- return "";
- }
- }
- }
- //定制该扩展列
- public class DataGridViewComboBoxExColumn : DataGridViewColumn
- {
- private object dataSoruce = null;
- public object DataSource
- {
- get { return dataSoruce; }
- set { dataSoruce = value; }
- }
- private string valueMember;
- public string ValueMember
- {
- get { return valueMember; }
- set { valueMember = value; }
- }
- private string displayMember;
- public string DisplayMember
- {
- get { return displayMember; }
- set { displayMember = value; }
- }
- public DataGridViewComboBoxExColumn()
- : base(new DataGridViewComboBoxExCell())
- {
- }
- public override DataGridViewCell CellTemplate
- {
- get
- {
- return base.CellTemplate;
- }
- set
- {
- if (value != null && !value.GetType().IsAssignableFrom(typeof(DataGridViewComboBoxExCell)))
- {
- throw new InvalidCastException("is not DataGridViewComboxExCell");
- }
- base.CellTemplate = value;
- }
- }
- private DataGridViewComboBoxExCell ComboBoxCellTemplate
- {
- get
- {
- return (DataGridViewComboBoxExCell)this.CellTemplate;
- }
- }
- }
- }