2014-02-10 43 views
0

我在layoutcontrol1下的表单上有许多Devexpress控件。我使用下面的代码 来遍历每个控件,并在用户单击“清除”按钮时清除现有数据。但是,它在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 
} 

有什么建议吗?

+1

是指这样一个问题:[?我们怎样才能清除的Win​​Form的所有表单控件] [1] 或 [什么是最好的方式清除上的形式C#所有控件] [2] 也许是有用的 [1]:http://stackoverflow.com/questions/14620375/how-can-we- clear-the-all-form-controls-on-winform [2]:http://stackoverflow.com/questions/297526/what-is-the-best-way-to-clear-all-controls-on -a-form -c –

回答

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(); 
} 
+0

谢谢你的回应。它像一个魅力。但是,当我将它应用于GridLookUpEdit时,它会引发异常。任何原因? – aby

+0

@aby这是非常奇怪的......你有什么确切的例外?无论如何,您可以直接联系DevExpress(https://www.devexpress.com/Support/Center)做出最终决定 – DmitryG

+0

它只是抛出异常“序列不包含任何元素”,但有数据绑定到GridLookUpEdit – aby