DataGridViewComboBoxColumn列,我下拉选择完一个单元格的值后焦点离开,值是存在的
当我再点回这个单元格的时侯,我选的值没了,会把下拉列表里的第一值赋上去,不知道怎么解决了
请高人指点。
20 个解决方案
#1
说明一下,这个 DataGridViewComboBoxColumn是我自已写的
根据微软提供的方案做的如下三个类:
DataGridViewComboBoxCell
DataGridViewComboBoxColumn
DataGridViewComboBoxEditingControl
根据微软提供的方案做的如下三个类:
DataGridViewComboBoxCell
DataGridViewComboBoxColumn
DataGridViewComboBoxEditingControl
#2
帮顶~
#3
多谢,你有做过吗?指点一下
#4
你是不是在构造函数里做初始化了?选中一个单元格的时候,DataGridView会根据这个列的类型创建一个Editor。如果你在构造函数里初始化,那肯定选中这个单元格的时候值会自动改变。
#5
那这块我怎么解决呢?在我那三个类中哪一个中修改?
#6
这个……呵呵,这个我也不是很熟悉,只是以前在一个类似DataGridView的第三方控件上搞过,原理上大概知道。
如果确实是我说的问题的话,我猜应该有Editor激活的事件之类,应该在这个地方将DataGridView当前单元格的内容赋值给Editor,你可以在这里添加一个初始化判断,比如单元格内容为DBNull,可以设置为第一个选项。
如果确实是我说的问题的话,我猜应该有Editor激活的事件之类,应该在这个地方将DataGridView当前单元格的内容赋值给Editor,你可以在这里添加一个初始化判断,比如单元格内容为DBNull,可以设置为第一个选项。
#7
嗯,好的,多谢你
#8
还有哪位兄弟做过,指点!
#9
现在就是当处于编辑状态的时侯值被重写了
重写成下拉列表的第一个值,怎么处理?
重写成下拉列表的第一个值,怎么处理?
#10
把你写的代码贴出来!!
或者单步调试一下!!
或者单步调试一下!!
#11
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Data;
namespace demo
{
public partial class demo_cbxCell : System.Windows.Forms.DataGridViewComboBoxCell
{
public demo_cbxCell()
{
}
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
if (value == null)
{
return DBNull.Value;
}
else
{
return value.ToString();
}
}
public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
{
//return base.ParseFormattedValue(formattedValue, cellStyle, formattedValueTypeConverter, valueTypeConverter);
if (formattedValue == null && formattedValue.ToString() == string.Empty)
{
return DBNull.Value;
}
else
{
return formattedValue.ToString();
}
}
public override Type EditType
{
get
{
return typeof(demo_cbxEditingControl);
}
}
}
}
using System.Windows.Forms;
using System.ComponentModel;
using System.Data;
namespace demo
{
public partial class demo_cbxCell : System.Windows.Forms.DataGridViewComboBoxCell
{
public demo_cbxCell()
{
}
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
if (value == null)
{
return DBNull.Value;
}
else
{
return value.ToString();
}
}
public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
{
//return base.ParseFormattedValue(formattedValue, cellStyle, formattedValueTypeConverter, valueTypeConverter);
if (formattedValue == null && formattedValue.ToString() == string.Empty)
{
return DBNull.Value;
}
else
{
return formattedValue.ToString();
}
}
public override Type EditType
{
get
{
return typeof(demo_cbxEditingControl);
}
}
}
}
#12
我觉得问题出在这个类上,其它的几个类一起送上
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Design;
namespace demo
{
public partial class demo_cbxColumn : System.Windows.Forms.DataGridViewColumn
{
public demo_cbxColumn()
: base()
{
this.CellTemplate = new demo_cbxCell();
}
bool m_AllowNull = true;
string m_CloumnName = null;
string m_valueFiled = String.Empty;
string m_ChinaFiled = String.Empty;
string m_UnChinaFiled = String.Empty;
static int index = 1;
#region 重载方法
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
if (value != null && !value.GetType().IsAssignableFrom(typeof(demo_cbxCell)))
{
throw new InvalidCastException("不是DataGridViewDataWindowCell");
}
base.CellTemplate = (demo_cbxCell)value;
}
}
public demo_cbxCell ComboBoxCellTemplate
{
get
{
return (demo_cbxCell)this.CellTemplate;
}
}
public override object Clone()
{
demo_cbxColumn col = (demo_cbxColumn)base.Clone();
col.AllowNull = this.m_AllowNull;
col.ChinaFiled = this.ChinaFiled;
col.UnChinaFiled = this.UnChinaFiled;
col.ValueFiled = this.ValueFiled;
col.ValueName = this.ValueName;
return col;
}
#endregion
#region 属性
public bool AllowNull
{
set
{
this.m_AllowNull = value;
if (m_AllowNull)
{
this.HeaderCell.Style.ForeColor = Color.Black;
}
else
{
this.HeaderCell.Style.ForeColor = Color.Red;
}
}
get { return this.m_AllowNull; }
}
public string ValueFiled
{
get
{
return m_valueFiled;
}
set
{
m_valueFiled = value;
}
}
public string ChinaFiled
{
get
{
return m_ChinaFiled;
}
set
{
m_ChinaFiled = value;
}
}
public string UnChinaFiled
{
get
{
return m_UnChinaFiled;
}
set
{
m_UnChinaFiled = value;
}
}
string m_valuename = String.Empty;
public string ValueName
{
get { return m_valuename; }
set
{
m_valuename = value;
}
}
object d;
public object DataSource
{
set { this.d = value; }
get { return this.d; }
}
#endregion
}
}
using System;
using System.Windows.Forms;
using System.Data;
namespace demo
{
public partial class demo_cbxEditingControl : demo.demo_combox, IDataGridViewEditingControl
{
//控件所在行
protected int rowIndex;
protected System.Windows.Forms.DataGridView dataGridView;
//是否有未提交的更改
protected bool valueChanged = false;
public demo_cbxEditingControl()
{
}
//重写基类
protected override void OnTextChanged(System.EventArgs e)
{
base.OnTextChanged(e);
NotifyDataGridViewOfValueChange();
}
// 当text值发生变化时,通知DataGridView
private void NotifyDataGridViewOfValueChange()
{
valueChanged = true;
dataGridView.NotifyCurrentCellDirty(true);
}
/// <summary>
/// 在Cell被编辑的时候光标显示
/// </summary>
public Cursor EditingPanelCursor
{
get
{
return Cursors.IBeam;
}
}
/// <summary>
/// 获取或设置所在的DataGridView
/// </summary>
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}
/// <summary>
/// 获取或设置格式化后的值
/// </summary>
public object EditingControlFormattedValue
{
set
{
Text = value.ToString();
NotifyDataGridViewOfValueChange();
}
get
{
return this.Text;
}
}
/// <summary>
/// 获取控件的Text值
/// </summary>
/// <param name="context">错误上下文</param>
/// <returns></returns>
public virtual object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return Text;
}
/// <summary>
/// 编辑键盘
/// </summary>
/// <param name="keyData"></param>
/// <param name="dataGridViewWantsInputKey"></param>
/// <returns></returns>
public bool EditingControlWantsInputKey(
Keys key, bool dataGridViewWantsInputKey)
{
// Let the DateTimePicker handle the keys listed.
switch (key & Keys.KeyCode)
{
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Home:
case Keys.End:
case Keys.PageDown:
case Keys.PageUp:
return true;
default:
return false;
}
}
public void PrepareEditingControlForEdit(bool selectAll)
{
}
public virtual bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}
/// <summary>
/// 控件所在行
/// </summary>
public int EditingControlRowIndex
{
get
{
return this.rowIndex;
}
set
{
this.rowIndex = value;
}
}
/// <summary>
/// 设置样式
/// </summary>
/// <param name="dataGridViewCellStyle"></param>
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.ForeColor = dataGridViewCellStyle.ForeColor;
this.BackColor = dataGridViewCellStyle.BackColor;
}
/// <summary>
/// 是否值发生了变化
/// </summary>
public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
this.valueChanged = value;
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Design;
namespace demo
{
public partial class demo_cbxColumn : System.Windows.Forms.DataGridViewColumn
{
public demo_cbxColumn()
: base()
{
this.CellTemplate = new demo_cbxCell();
}
bool m_AllowNull = true;
string m_CloumnName = null;
string m_valueFiled = String.Empty;
string m_ChinaFiled = String.Empty;
string m_UnChinaFiled = String.Empty;
static int index = 1;
#region 重载方法
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
if (value != null && !value.GetType().IsAssignableFrom(typeof(demo_cbxCell)))
{
throw new InvalidCastException("不是DataGridViewDataWindowCell");
}
base.CellTemplate = (demo_cbxCell)value;
}
}
public demo_cbxCell ComboBoxCellTemplate
{
get
{
return (demo_cbxCell)this.CellTemplate;
}
}
public override object Clone()
{
demo_cbxColumn col = (demo_cbxColumn)base.Clone();
col.AllowNull = this.m_AllowNull;
col.ChinaFiled = this.ChinaFiled;
col.UnChinaFiled = this.UnChinaFiled;
col.ValueFiled = this.ValueFiled;
col.ValueName = this.ValueName;
return col;
}
#endregion
#region 属性
public bool AllowNull
{
set
{
this.m_AllowNull = value;
if (m_AllowNull)
{
this.HeaderCell.Style.ForeColor = Color.Black;
}
else
{
this.HeaderCell.Style.ForeColor = Color.Red;
}
}
get { return this.m_AllowNull; }
}
public string ValueFiled
{
get
{
return m_valueFiled;
}
set
{
m_valueFiled = value;
}
}
public string ChinaFiled
{
get
{
return m_ChinaFiled;
}
set
{
m_ChinaFiled = value;
}
}
public string UnChinaFiled
{
get
{
return m_UnChinaFiled;
}
set
{
m_UnChinaFiled = value;
}
}
string m_valuename = String.Empty;
public string ValueName
{
get { return m_valuename; }
set
{
m_valuename = value;
}
}
object d;
public object DataSource
{
set { this.d = value; }
get { return this.d; }
}
#endregion
}
}
using System;
using System.Windows.Forms;
using System.Data;
namespace demo
{
public partial class demo_cbxEditingControl : demo.demo_combox, IDataGridViewEditingControl
{
//控件所在行
protected int rowIndex;
protected System.Windows.Forms.DataGridView dataGridView;
//是否有未提交的更改
protected bool valueChanged = false;
public demo_cbxEditingControl()
{
}
//重写基类
protected override void OnTextChanged(System.EventArgs e)
{
base.OnTextChanged(e);
NotifyDataGridViewOfValueChange();
}
// 当text值发生变化时,通知DataGridView
private void NotifyDataGridViewOfValueChange()
{
valueChanged = true;
dataGridView.NotifyCurrentCellDirty(true);
}
/// <summary>
/// 在Cell被编辑的时候光标显示
/// </summary>
public Cursor EditingPanelCursor
{
get
{
return Cursors.IBeam;
}
}
/// <summary>
/// 获取或设置所在的DataGridView
/// </summary>
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}
/// <summary>
/// 获取或设置格式化后的值
/// </summary>
public object EditingControlFormattedValue
{
set
{
Text = value.ToString();
NotifyDataGridViewOfValueChange();
}
get
{
return this.Text;
}
}
/// <summary>
/// 获取控件的Text值
/// </summary>
/// <param name="context">错误上下文</param>
/// <returns></returns>
public virtual object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return Text;
}
/// <summary>
/// 编辑键盘
/// </summary>
/// <param name="keyData"></param>
/// <param name="dataGridViewWantsInputKey"></param>
/// <returns></returns>
public bool EditingControlWantsInputKey(
Keys key, bool dataGridViewWantsInputKey)
{
// Let the DateTimePicker handle the keys listed.
switch (key & Keys.KeyCode)
{
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Home:
case Keys.End:
case Keys.PageDown:
case Keys.PageUp:
return true;
default:
return false;
}
}
public void PrepareEditingControlForEdit(bool selectAll)
{
}
public virtual bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}
/// <summary>
/// 控件所在行
/// </summary>
public int EditingControlRowIndex
{
get
{
return this.rowIndex;
}
set
{
this.rowIndex = value;
}
}
/// <summary>
/// 设置样式
/// </summary>
/// <param name="dataGridViewCellStyle"></param>
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.ForeColor = dataGridViewCellStyle.ForeColor;
this.BackColor = dataGridViewCellStyle.BackColor;
}
/// <summary>
/// 是否值发生了变化
/// </summary>
public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
this.valueChanged = value;
}
}
}
}
#13
顶起
#14
焦点回去的时候,值为空了,可能是你没有记录原来的值!
#15
是的,这块怎么处理呢
我是一点耐心没了
#16
顶起
#17
帮顶~
#18
顶起,我已经发了好几个帖了,请大家帮帮我吧!
#19
再顶起
#20
顶起
#21
#1
说明一下,这个 DataGridViewComboBoxColumn是我自已写的
根据微软提供的方案做的如下三个类:
DataGridViewComboBoxCell
DataGridViewComboBoxColumn
DataGridViewComboBoxEditingControl
根据微软提供的方案做的如下三个类:
DataGridViewComboBoxCell
DataGridViewComboBoxColumn
DataGridViewComboBoxEditingControl
#2
帮顶~
#3
多谢,你有做过吗?指点一下
#4
你是不是在构造函数里做初始化了?选中一个单元格的时候,DataGridView会根据这个列的类型创建一个Editor。如果你在构造函数里初始化,那肯定选中这个单元格的时候值会自动改变。
#5
那这块我怎么解决呢?在我那三个类中哪一个中修改?
#6
这个……呵呵,这个我也不是很熟悉,只是以前在一个类似DataGridView的第三方控件上搞过,原理上大概知道。
如果确实是我说的问题的话,我猜应该有Editor激活的事件之类,应该在这个地方将DataGridView当前单元格的内容赋值给Editor,你可以在这里添加一个初始化判断,比如单元格内容为DBNull,可以设置为第一个选项。
如果确实是我说的问题的话,我猜应该有Editor激活的事件之类,应该在这个地方将DataGridView当前单元格的内容赋值给Editor,你可以在这里添加一个初始化判断,比如单元格内容为DBNull,可以设置为第一个选项。
#7
嗯,好的,多谢你
#8
还有哪位兄弟做过,指点!
#9
现在就是当处于编辑状态的时侯值被重写了
重写成下拉列表的第一个值,怎么处理?
重写成下拉列表的第一个值,怎么处理?
#10
把你写的代码贴出来!!
或者单步调试一下!!
或者单步调试一下!!
#11
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Data;
namespace demo
{
public partial class demo_cbxCell : System.Windows.Forms.DataGridViewComboBoxCell
{
public demo_cbxCell()
{
}
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
if (value == null)
{
return DBNull.Value;
}
else
{
return value.ToString();
}
}
public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
{
//return base.ParseFormattedValue(formattedValue, cellStyle, formattedValueTypeConverter, valueTypeConverter);
if (formattedValue == null && formattedValue.ToString() == string.Empty)
{
return DBNull.Value;
}
else
{
return formattedValue.ToString();
}
}
public override Type EditType
{
get
{
return typeof(demo_cbxEditingControl);
}
}
}
}
using System.Windows.Forms;
using System.ComponentModel;
using System.Data;
namespace demo
{
public partial class demo_cbxCell : System.Windows.Forms.DataGridViewComboBoxCell
{
public demo_cbxCell()
{
}
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
if (value == null)
{
return DBNull.Value;
}
else
{
return value.ToString();
}
}
public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
{
//return base.ParseFormattedValue(formattedValue, cellStyle, formattedValueTypeConverter, valueTypeConverter);
if (formattedValue == null && formattedValue.ToString() == string.Empty)
{
return DBNull.Value;
}
else
{
return formattedValue.ToString();
}
}
public override Type EditType
{
get
{
return typeof(demo_cbxEditingControl);
}
}
}
}
#12
我觉得问题出在这个类上,其它的几个类一起送上
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Design;
namespace demo
{
public partial class demo_cbxColumn : System.Windows.Forms.DataGridViewColumn
{
public demo_cbxColumn()
: base()
{
this.CellTemplate = new demo_cbxCell();
}
bool m_AllowNull = true;
string m_CloumnName = null;
string m_valueFiled = String.Empty;
string m_ChinaFiled = String.Empty;
string m_UnChinaFiled = String.Empty;
static int index = 1;
#region 重载方法
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
if (value != null && !value.GetType().IsAssignableFrom(typeof(demo_cbxCell)))
{
throw new InvalidCastException("不是DataGridViewDataWindowCell");
}
base.CellTemplate = (demo_cbxCell)value;
}
}
public demo_cbxCell ComboBoxCellTemplate
{
get
{
return (demo_cbxCell)this.CellTemplate;
}
}
public override object Clone()
{
demo_cbxColumn col = (demo_cbxColumn)base.Clone();
col.AllowNull = this.m_AllowNull;
col.ChinaFiled = this.ChinaFiled;
col.UnChinaFiled = this.UnChinaFiled;
col.ValueFiled = this.ValueFiled;
col.ValueName = this.ValueName;
return col;
}
#endregion
#region 属性
public bool AllowNull
{
set
{
this.m_AllowNull = value;
if (m_AllowNull)
{
this.HeaderCell.Style.ForeColor = Color.Black;
}
else
{
this.HeaderCell.Style.ForeColor = Color.Red;
}
}
get { return this.m_AllowNull; }
}
public string ValueFiled
{
get
{
return m_valueFiled;
}
set
{
m_valueFiled = value;
}
}
public string ChinaFiled
{
get
{
return m_ChinaFiled;
}
set
{
m_ChinaFiled = value;
}
}
public string UnChinaFiled
{
get
{
return m_UnChinaFiled;
}
set
{
m_UnChinaFiled = value;
}
}
string m_valuename = String.Empty;
public string ValueName
{
get { return m_valuename; }
set
{
m_valuename = value;
}
}
object d;
public object DataSource
{
set { this.d = value; }
get { return this.d; }
}
#endregion
}
}
using System;
using System.Windows.Forms;
using System.Data;
namespace demo
{
public partial class demo_cbxEditingControl : demo.demo_combox, IDataGridViewEditingControl
{
//控件所在行
protected int rowIndex;
protected System.Windows.Forms.DataGridView dataGridView;
//是否有未提交的更改
protected bool valueChanged = false;
public demo_cbxEditingControl()
{
}
//重写基类
protected override void OnTextChanged(System.EventArgs e)
{
base.OnTextChanged(e);
NotifyDataGridViewOfValueChange();
}
// 当text值发生变化时,通知DataGridView
private void NotifyDataGridViewOfValueChange()
{
valueChanged = true;
dataGridView.NotifyCurrentCellDirty(true);
}
/// <summary>
/// 在Cell被编辑的时候光标显示
/// </summary>
public Cursor EditingPanelCursor
{
get
{
return Cursors.IBeam;
}
}
/// <summary>
/// 获取或设置所在的DataGridView
/// </summary>
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}
/// <summary>
/// 获取或设置格式化后的值
/// </summary>
public object EditingControlFormattedValue
{
set
{
Text = value.ToString();
NotifyDataGridViewOfValueChange();
}
get
{
return this.Text;
}
}
/// <summary>
/// 获取控件的Text值
/// </summary>
/// <param name="context">错误上下文</param>
/// <returns></returns>
public virtual object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return Text;
}
/// <summary>
/// 编辑键盘
/// </summary>
/// <param name="keyData"></param>
/// <param name="dataGridViewWantsInputKey"></param>
/// <returns></returns>
public bool EditingControlWantsInputKey(
Keys key, bool dataGridViewWantsInputKey)
{
// Let the DateTimePicker handle the keys listed.
switch (key & Keys.KeyCode)
{
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Home:
case Keys.End:
case Keys.PageDown:
case Keys.PageUp:
return true;
default:
return false;
}
}
public void PrepareEditingControlForEdit(bool selectAll)
{
}
public virtual bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}
/// <summary>
/// 控件所在行
/// </summary>
public int EditingControlRowIndex
{
get
{
return this.rowIndex;
}
set
{
this.rowIndex = value;
}
}
/// <summary>
/// 设置样式
/// </summary>
/// <param name="dataGridViewCellStyle"></param>
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.ForeColor = dataGridViewCellStyle.ForeColor;
this.BackColor = dataGridViewCellStyle.BackColor;
}
/// <summary>
/// 是否值发生了变化
/// </summary>
public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
this.valueChanged = value;
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Design;
namespace demo
{
public partial class demo_cbxColumn : System.Windows.Forms.DataGridViewColumn
{
public demo_cbxColumn()
: base()
{
this.CellTemplate = new demo_cbxCell();
}
bool m_AllowNull = true;
string m_CloumnName = null;
string m_valueFiled = String.Empty;
string m_ChinaFiled = String.Empty;
string m_UnChinaFiled = String.Empty;
static int index = 1;
#region 重载方法
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
if (value != null && !value.GetType().IsAssignableFrom(typeof(demo_cbxCell)))
{
throw new InvalidCastException("不是DataGridViewDataWindowCell");
}
base.CellTemplate = (demo_cbxCell)value;
}
}
public demo_cbxCell ComboBoxCellTemplate
{
get
{
return (demo_cbxCell)this.CellTemplate;
}
}
public override object Clone()
{
demo_cbxColumn col = (demo_cbxColumn)base.Clone();
col.AllowNull = this.m_AllowNull;
col.ChinaFiled = this.ChinaFiled;
col.UnChinaFiled = this.UnChinaFiled;
col.ValueFiled = this.ValueFiled;
col.ValueName = this.ValueName;
return col;
}
#endregion
#region 属性
public bool AllowNull
{
set
{
this.m_AllowNull = value;
if (m_AllowNull)
{
this.HeaderCell.Style.ForeColor = Color.Black;
}
else
{
this.HeaderCell.Style.ForeColor = Color.Red;
}
}
get { return this.m_AllowNull; }
}
public string ValueFiled
{
get
{
return m_valueFiled;
}
set
{
m_valueFiled = value;
}
}
public string ChinaFiled
{
get
{
return m_ChinaFiled;
}
set
{
m_ChinaFiled = value;
}
}
public string UnChinaFiled
{
get
{
return m_UnChinaFiled;
}
set
{
m_UnChinaFiled = value;
}
}
string m_valuename = String.Empty;
public string ValueName
{
get { return m_valuename; }
set
{
m_valuename = value;
}
}
object d;
public object DataSource
{
set { this.d = value; }
get { return this.d; }
}
#endregion
}
}
using System;
using System.Windows.Forms;
using System.Data;
namespace demo
{
public partial class demo_cbxEditingControl : demo.demo_combox, IDataGridViewEditingControl
{
//控件所在行
protected int rowIndex;
protected System.Windows.Forms.DataGridView dataGridView;
//是否有未提交的更改
protected bool valueChanged = false;
public demo_cbxEditingControl()
{
}
//重写基类
protected override void OnTextChanged(System.EventArgs e)
{
base.OnTextChanged(e);
NotifyDataGridViewOfValueChange();
}
// 当text值发生变化时,通知DataGridView
private void NotifyDataGridViewOfValueChange()
{
valueChanged = true;
dataGridView.NotifyCurrentCellDirty(true);
}
/// <summary>
/// 在Cell被编辑的时候光标显示
/// </summary>
public Cursor EditingPanelCursor
{
get
{
return Cursors.IBeam;
}
}
/// <summary>
/// 获取或设置所在的DataGridView
/// </summary>
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}
/// <summary>
/// 获取或设置格式化后的值
/// </summary>
public object EditingControlFormattedValue
{
set
{
Text = value.ToString();
NotifyDataGridViewOfValueChange();
}
get
{
return this.Text;
}
}
/// <summary>
/// 获取控件的Text值
/// </summary>
/// <param name="context">错误上下文</param>
/// <returns></returns>
public virtual object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return Text;
}
/// <summary>
/// 编辑键盘
/// </summary>
/// <param name="keyData"></param>
/// <param name="dataGridViewWantsInputKey"></param>
/// <returns></returns>
public bool EditingControlWantsInputKey(
Keys key, bool dataGridViewWantsInputKey)
{
// Let the DateTimePicker handle the keys listed.
switch (key & Keys.KeyCode)
{
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Home:
case Keys.End:
case Keys.PageDown:
case Keys.PageUp:
return true;
default:
return false;
}
}
public void PrepareEditingControlForEdit(bool selectAll)
{
}
public virtual bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}
/// <summary>
/// 控件所在行
/// </summary>
public int EditingControlRowIndex
{
get
{
return this.rowIndex;
}
set
{
this.rowIndex = value;
}
}
/// <summary>
/// 设置样式
/// </summary>
/// <param name="dataGridViewCellStyle"></param>
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.ForeColor = dataGridViewCellStyle.ForeColor;
this.BackColor = dataGridViewCellStyle.BackColor;
}
/// <summary>
/// 是否值发生了变化
/// </summary>
public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
this.valueChanged = value;
}
}
}
}
#13
顶起
#14
焦点回去的时候,值为空了,可能是你没有记录原来的值!
#15
是的,这块怎么处理呢
我是一点耐心没了
#16
顶起
#17
帮顶~
#18
顶起,我已经发了好几个帖了,请大家帮帮我吧!
#19
再顶起
#20
顶起