在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;
}
}