【问题标题】:How to clear form data如何清除表单数据
【发布时间】:2014-03-07 12:22:49
【问题描述】:

我的表单上有许多 Devexpress 控件,位于 layoutcontrol1 下。我用了 下面的代码遍历每个控件并在用户单击“清除”按钮时清除现有数据。但是,它不适用于 LookupEdit(将 EditValue 设置为 0)和 CheckedListBoxControl(取消选中所有选定的项目)。

foreach (Control c in layoutControl1.Controls)
{
   if (c.GetType() == typeof(TextEdit) || c.GetType()==typeof(MemoEdit))
       c.Text = String.Empty;

   if (c.GetType() == typeof(LookUpEdit))
       c.EditValue = 0; //doesn't have EditValue property

   if (c.GetType() == typeof(CheckedListBoxControl))
       c.CheckedItems = CheckState.Unchecked; //doesn't have such property
}

有什么建议吗?

【问题讨论】:

标签: c# winforms user-controls devexpress


【解决方案1】:

只需尝试以下操作:

foreach(Control c in layoutControl1.Controls) {
    var edit = c as DevExpress.XtraEditors.BaseEdit; // base class for DX editors
    if(edit != null)
        edit.EditValue = null;
    var listBox = c as DevExpress.XtraEditors.CheckedListBoxControl;
    if(listBox != null) 
        listBox.UnCheckAll();
}

【讨论】:

  • 感谢您的回复。它就像一个魅力。但是,当我将其应用于 GridLookUpEdit 时,它会引发异常。有什么原因吗?
  • @aby 这很奇怪......你有什么确切的例外?无论如何,您可以联系DevExpressdirectly做出最终决定
  • 它只是抛出异常“序列不包含元素”但是有数据绑定到 GridLookUpEdit
  • 有时“对象引用未设置为对象的实例”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-07
  • 2014-02-18
  • 2016-03-11
  • 1970-01-01
  • 1970-01-01
  • 2020-07-26
  • 2021-09-24
相关资源
最近更新 更多