您的位置:首页 > 其它

看了一篇介绍如何自定义datagridview列的文章,写了一个可输入的combobox列

2010-11-17 08:53 399 查看
这篇文章介绍了定制列:构建DataGridView的定制NumericUpDown单元格(Cell)和表格列(Column)

下面是可输入的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

}


本文中的代码或引用的代码仅供学习讨论之用,未经严格测试。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐