DataGridViewComboBoxExEditingControl.cs
上传用户:b2s168
上传日期:2021-04-20
资源大小:45k
文件大小:6k
源码类别:

组合框控件

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. namespace CombEdit
  6. {
  7.     //设计一个继承自ComboBox的下拉框编辑列组件
  8.     public class DataGridViewComboBoxExEditingControl : ComboBox, IDataGridViewEditingControl
  9.     {
  10.         protected int rowIndex;
  11.         protected DataGridView dataGridView;
  12.         protected bool valueChanged = false;
  13.         protected override void OnTextChanged(System.EventArgs e)
  14.         {
  15.             base.OnTextChanged(e);
  16.             NotifyDataGridViewOfValueChange();
  17.         }
  18.         private void NotifyDataGridViewOfValueChange()
  19.         {
  20.             valueChanged = true;
  21.             dataGridView.NotifyCurrentCellDirty(true);
  22.         }
  23.         protected override void OnSelectedIndexChanged(EventArgs e)
  24.         {
  25.             base.OnTextChanged(e);
  26.             NotifyDataGridViewOfValueChange();
  27.         }
  28.         public Cursor EditingPanelCursor
  29.         {
  30.             get { return Cursors.IBeam; }
  31.         }
  32.         public DataGridView EditingControlDataGridView
  33.         {
  34.             get { return dataGridView; }
  35.             set { dataGridView = value; }
  36.         }
  37.         public object EditingControlFormattedValue
  38.         {
  39.             set
  40.             {
  41.                 Text = value.ToString();
  42.                 NotifyDataGridViewOfValueChange();
  43.             }
  44.             get
  45.             {
  46.                 return this.Text;
  47.             }
  48.         }
  49.         public virtual object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
  50.         {
  51.             return Text;
  52.         }
  53.         public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
  54.         {
  55.             switch (key & Keys.KeyCode)
  56.             {
  57.                 case Keys.Left:
  58.                 case Keys.Up:
  59.                 case Keys.Down:
  60.                 case Keys.Right:
  61.                 case Keys.Home:
  62.                 case Keys.End:
  63.                 case Keys.Escape:
  64.                 case Keys.Enter:
  65.                 case Keys.PageDown:
  66.                 case Keys.PageUp:
  67.                     return true;
  68.                 default:
  69.                     return false;
  70.             }
  71.         }
  72.         public void PrepareEditingControlForEdit(bool selectAll)
  73.         {
  74.             if (selectAll)
  75.             {
  76.                 SelectAll();
  77.             }
  78.             else
  79.             {
  80.                 this.SelectionStart = this.ToString().Length;
  81.             }
  82.         }
  83.         public virtual bool RepositionEditingControlOnValueChange
  84.         {
  85.             get
  86.             {
  87.                 return false;
  88.             }
  89.         }
  90.         public int EditingControlRowIndex
  91.         {
  92.             get { return this.rowIndex; }
  93.             set { this.rowIndex = value; }
  94.         }
  95.         public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
  96.         {
  97.             this.Font = dataGridViewCellStyle.Font;
  98.             this.ForeColor = dataGridViewCellStyle.ForeColor;
  99.             this.BackColor = dataGridViewCellStyle.BackColor;
  100.         }
  101.         public bool EditingControlValueChanged
  102.         {
  103.             get { return valueChanged; }
  104.             set { this.valueChanged = value; }
  105.         }
  106.     }
  107.     //定制该扩展列的单元格
  108.     public class DataGridViewComboBoxExCell : DataGridViewTextBoxCell
  109.     {
  110.         public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
  111.         {
  112.             base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
  113.             DataGridViewComboBoxExEditingControl clt = DataGridView.EditingControl as DataGridViewComboBoxExEditingControl;
  114.             DataGridViewComboBoxExColumn col = (DataGridViewComboBoxExColumn)OwningColumn;
  115.             clt.DataSource = col.DataSource;
  116.             clt.DisplayMember = col.DisplayMember;
  117.             clt.ValueMember = col.ValueMember;
  118.             clt.Text = Convert.ToString(this.Value);
  119.         }
  120.         public override Type EditType
  121.         {
  122.             get
  123.             {
  124.                 return typeof(DataGridViewComboBoxExEditingControl);
  125.             }
  126.         }
  127.         public override Type ValueType
  128.         {
  129.             get
  130.             {
  131.                 return typeof(string);
  132.             }
  133.         }
  134.         public override object DefaultNewRowValue
  135.         {
  136.             get
  137.             {
  138.                 return "";
  139.             }
  140.         }
  141.     }
  142.     //定制该扩展列
  143.     public class DataGridViewComboBoxExColumn : DataGridViewColumn
  144.     {
  145.         private object dataSoruce = null;
  146.         public object DataSource
  147.         {
  148.             get { return dataSoruce; }
  149.             set { dataSoruce = value; }
  150.         }
  151.         private string valueMember;
  152.         public string ValueMember
  153.         {
  154.             get { return valueMember; }
  155.             set { valueMember = value; }
  156.         }
  157.         private string displayMember;
  158.         public string DisplayMember
  159.         {
  160.             get { return displayMember; }
  161.             set { displayMember = value; }
  162.         }
  163.         public DataGridViewComboBoxExColumn()
  164.             : base(new DataGridViewComboBoxExCell())
  165.         {
  166.         }
  167.         public override DataGridViewCell CellTemplate
  168.         {
  169.             get
  170.             {
  171.                 return base.CellTemplate;
  172.             }
  173.             set
  174.             {
  175.                 if (value != null && !value.GetType().IsAssignableFrom(typeof(DataGridViewComboBoxExCell)))
  176.                 {
  177.                     throw new InvalidCastException("is not DataGridViewComboxExCell");
  178.                 }
  179.                 base.CellTemplate = value;
  180.             }
  181.         }
  182.         private DataGridViewComboBoxExCell ComboBoxCellTemplate
  183.         {
  184.             get
  185.             {
  186.                 return (DataGridViewComboBoxExCell)this.CellTemplate;
  187.             }
  188.         }
  189.     }
  190. }