TextBox 绑定表的数字类型字段,清除文本框内容时,焦点不能移出文本框的解决方法
代码
Binding bind = new Binding("Text", bindsourse, name, true, DataSourceUpdateMode.OnValidation, DBNull.Value);
tb.DataBindings.Add(bind);
bind.DataSourceNullValue = DBNull.Value;
if (((System.Data.DataView)bindsourse.DataSource).Table.Columns[name].DataType.FullName == "System.Decimal")
{
//下面两句用于处理数值型字段绑定无法设置空值问题
bind.Format += new ConvertEventHandler(bind_Format);
bind.Parse += new ConvertEventHandler(bind_Parse);
}
void bind_Format(object sender, ConvertEventArgs e)//绑定控件时发生
{
if (e.Value == DBNull.Value)
e.Value = "";
}
void bind_Parse(object sender, ConvertEventArgs e)//控件值更改时发生
{
if (e.Value.ToString() == "")
e.Value = DBNull.Value;
}
tb.DataBindings.Add(bind);
bind.DataSourceNullValue = DBNull.Value;
if (((System.Data.DataView)bindsourse.DataSource).Table.Columns[name].DataType.FullName == "System.Decimal")
{
//下面两句用于处理数值型字段绑定无法设置空值问题
bind.Format += new ConvertEventHandler(bind_Format);
bind.Parse += new ConvertEventHandler(bind_Parse);
}
void bind_Format(object sender, ConvertEventArgs e)//绑定控件时发生
{
if (e.Value == DBNull.Value)
e.Value = "";
}
void bind_Parse(object sender, ConvertEventArgs e)//控件值更改时发生
{
if (e.Value.ToString() == "")
e.Value = DBNull.Value;
}