2014-02-10 42 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