看了一篇介绍如何自定义datagridview列的文章,写了一个可输入的combobox列
2010-11-17 08:53
399 查看
这篇文章介绍了定制列:构建DataGridView的定制NumericUpDown单元格(Cell)和表格列(Column)
下面是可输入的combobox列的代码,将其保存到项目的cs文件中,在datagridview控件的列属性中ColumnType里就可以到DataGridViewMyComboBoxColumn供选择了。
本文中的代码或引用的代码仅供学习讨论之用,未经严格测试。
下面是可输入的combobox列的代码,将其保存到项目的cs文件中,在datagridview控件的列属性中ColumnType里就可以到DataGridViewMyComboBoxColumn供选择了。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Converter.sys { #region C# DataGridView combobox可编辑;由于用户可输入任何值,所以不区分值和显示值 /// <summary> /// /// </summary> public class DataGridViewMyComboBoxColumn : DataGridViewComboBoxColumn { public DataGridViewMyComboBoxColumn() { DataGridViewMyComboEditBoxCell obj = new DataGridViewMyComboEditBoxCell(); this.CellTemplate = obj; } } public class DataGridViewMyComboEditBoxCell : DataGridViewComboBoxCell { public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle) { base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle); ComboBox comboBox = (ComboBox)base.DataGridView.EditingControl; if (comboBox != null) { comboBox.DropDownStyle = ComboBoxStyle.DropDown; comboBox.AutoCompleteMode = AutoCompleteMode.Suggest; } } protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context) { return value; } public override Type EditType { get { return typeof(DataGridViewMyComboBoxEditingControl); } } public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter) { return formattedValue; } } public class DataGridViewMyComboBoxEditingControl : DataGridViewComboBoxEditingControl { // The grid that owns this editing control //private DataGridView dataGridView; // Stores whether the editing control's value has changed or not private bool valueChanged=false; /// <summary> /// Property which indicates whether the value of the editing control has changed or not /// </summary> public override bool EditingControlValueChanged { get { return this.valueChanged; } set { this.valueChanged = value; } } protected override void OnValidating(CancelEventArgs e) { } /// <summary> /// Returns the current value of the editing control. /// </summary> public override object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context) { return this.Text; } /// <summary> /// Property which represents the current formatted value of the editing control /// </summary> public override object EditingControlFormattedValue { get { return GetEditingControlFormattedValue(DataGridViewDataErrorContexts.Formatting); } set { string newValue = value as string; if (newValue != null) { this.Text = newValue; } } } /// <summary> /// Listen to the KeyPress notification to know when the value changed, and /// notify the grid of the change. /// </summary> protected override void OnKeyPress(KeyPressEventArgs e) { base.OnKeyPress(e); if (true) { NotifyDataGridViewOfValueChange(); // Let the DataGridView know about the value change } } /// <summary> /// Small utility function that updates the local dirty state and /// notifies the grid of the value change. /// </summary> private void NotifyDataGridViewOfValueChange() { if (!this.valueChanged) { this.valueChanged = true; if (this.EditingControlDataGridView != null) { this.EditingControlDataGridView.NotifyCurrentCellDirty(true); } } } } #endregion }
本文中的代码或引用的代码仅供学习讨论之用,未经严格测试。
相关文章推荐
- C# 如何在一个Datagridview 的 某一个cell里面 绑定一个自定义的combobox
- 一篇对如何成为一个优秀程序员有指导意义的好文章,初学者必读!
- MyBatis: 获取一个用户的所有blog的基础上讲一下如何获取一篇文章机器作者的信息。
- 一篇介绍如何使用XAMPP以及PDT开发PHP环境搭建的文章
- 上篇文章介绍如何在settings中增加一个新模块,那如何隐藏一个已经存在的模块呢?
- 引用自高手(http://www.cnblogs.com/coolalan/p/4263652.)---一篇如何使用csdn git的文章(作为一个混惯了svn的码农,这个真是改三观的好东西)!!!!
- 当你输入一个网址的时候,实际会发生什么?(很好的一篇文章)
- mfc中,COMBOBOX如何获取输入的文本内容
- 如何用angularjs制作一个完整的表格之四__自定义ng-model标签的属性使其支持input之外的html元素
- 标题:如何在dropDownList中实现既能输入一个新值,又能实现下拉选
- 如何更好地限制一个UITextField的输入长度
- JAVA入门学习-学习如何自定义一个实体类,用于存储一组数据。
- 如何自定义一个vue.js的过滤器?
- 一篇介绍Reportt Builder的文章
- 如何将一篇图文并茂的文章截取指定…
- 一篇介绍DirectX 10 的文章
- WTL for MFC Programming实践篇 --- 一个自定义ComboBox的移植过程(上)
- 如何在MDT/BDD中写一个自定义的脚本(1)
- 今天上午看到一篇非常好的,介绍 抽象与接口区别的文章,特别收藏
- 一篇不错的hadoop介绍文章