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

组合框控件

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.ComponentModel;
  6. namespace CombEdit
  7. {
  8.     public class DataGridViewComboEditBoxCell : DataGridViewComboBoxCell
  9.     {
  10.         public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
  11.         {
  12.             base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
  13.             ComboBox comboBox = base.DataGridView.EditingControl as ComboBox;
  14.             if (comboBox != null)
  15.             {
  16.                 comboBox.DropDownStyle = ComboBoxStyle.DropDown;
  17.                 comboBox.Validating += new CancelEventHandler(comboBox_Validating);
  18.             }
  19.         }
  20.         protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
  21.         {
  22.             if (value != null && DataSource==null)
  23.             {
  24.                 if (value.ToString().Trim() != string.Empty)
  25.                 {
  26.                     if (Items.IndexOf(value) == -1)
  27.                     {
  28.                         Items.Add(value);
  29.                         DataGridViewComboBoxColumn col = OwningColumn as DataGridViewComboBoxColumn;
  30.                         col.Items.Add(value);
  31.                     }
  32.                 }
  33.             }
  34.             return base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);
  35.         }
  36.         void comboBox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
  37.         {
  38.             DataGridViewComboBoxEditingControl cbo = sender as DataGridViewComboBoxEditingControl;
  39.             if (cbo.Text.Trim() == string.Empty) return;
  40.             DataGridView grid = cbo.EditingControlDataGridView;
  41.             object value = cbo.Text;
  42.             // Add value to list if not there
  43.             if (cbo.Items.IndexOf(value) == -1)
  44.             {
  45.                 DataGridViewComboBoxColumn cboCol = grid.Columns[grid.CurrentCell.ColumnIndex] as DataGridViewComboBoxColumn;
  46.                 // Must add to both the current combobox as well as the template, to avoid duplicate entries
  47.                 if (DataSource == null)
  48.                 {
  49.                     cbo.Items.Add(value);
  50.                     cboCol.Items.Add(value);
  51.                     grid.CurrentCell.Value = value;
  52.                 }                
  53.             }
  54.         }
  55.     }
  56.     public class DataGridViewComboEditBoxColumn : DataGridViewComboBoxColumn
  57.     {
  58.         public DataGridViewComboEditBoxColumn()
  59.         {
  60.             DataGridViewComboEditBoxCell obj = new DataGridViewComboEditBoxCell();
  61.             this.CellTemplate = obj;
  62.         }
  63.     }
  64.  
  65. }