关于Devexpress xtraGrid中数字字段删除数字后出现输入字符串格式不正确问题的解决方案
在DevExpress开发中经常遇到xtraGrid绑定数据在剪切或删除单元格数据后出现“输入字符串格式不正确”的提示,很烦恼!
解决前效果:
解决后效果:
1.新建RepositoryItemTextEdit并将其绑定到列
[csharp]
DevExpress.XtraEditors.Repository.RepositoryItemTextEdit dtAfter = new DevExpress.XtraEditors.Repository.RepositoryItemTextEdit();
dtAfter.ParseEditValue += new DevExpress.XtraEditors.Controls.ConvertEditValueEventHandler(dtText_ParseEditValue);
gridview.Columns[stringInfo[0].ToString()].ColumnEdit = dtAfter;
dtAfter.EditFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
dtAfter.EditFormat.FormatString = "{0:N3}";
dtAfter.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
dtAfter.DisplayFormat.FormatString = "{0:N3}";
2.实现dtText_ParseEditValue事件
[csharp]
void dtText_ParseEditValue(object sender, DevExpress.XtraEditors.Controls.ConvertEditValueEventArgs e)
{
TextEdit edit = sender as TextEdit;
object obj = e.Value;
if (edit.Text == string.Empty)
{
if (edit.EditValue == null || edit.EditValue.ToString() == "")
{
e.Value = null;
}
}
else
{
e.Value = obj;
}
}
补充:软件开发 , C# ,