2016-02-09 56 views
2

我有一個前端應用程序,而不是立即刪除對象,我們在每個對象上都有一個標誌,表明它是否應該被刪除,所以它可以是以後處理。所以問題是當我在WinForms中使用DataGrid的前端對象時。當我設置Deleted標誌時,我希望該對象不會顯示在DataGrid中,並且將BindingList作爲DataGrid的DataSource。每當DataGrid重新繪製時,是否有辦法強制過濾器?這是DataGrid的功能嗎?或者BindingList的功能>?對於那些更可視化的人來說,這是一個代碼示例。 (警告這是出於概念目的的代碼示例)在WinForms中用BindingList <>處理已刪除的標誌

test.cs中

public class Person : INotifyProperyChanged 
{ 
    public string Name { get; set; } 
    public int Id { get; set; } 
    private bool _isForDelete; 
    public bool IsForDelete 
    { 
     get { return _isForDelete; } 
     set 
     { 
      _isForDelete = value; 
      OnPropertyChanged("IsForDelete") 
     } 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 
    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

MyForm.cs

private BindingList<Person> _persons; 

private void MyForm_Load(object sender, EventArgs e) 
{ 
    _persons = GetPersonsFromServer(); 
    //Obviously this doesn't work, but I can dream. This is the basic idea. 
    _myDataGrid.DataSource = _persons.Where(x=>!x.IsForDelete); 
} 

private void DeleteBtn_Click(object sender, EventArgs e) 
{ 
    foreach(var row in _myDataGrid.SelectedRows) 
    { 
     var person = (Person)row.DataBoundItem; 
     person.IsForDelete = true; 
    } 
} 

任何建議?

回答

0

你的問題的一個解決方案是循環訪問datagrid的每一行,獲取綁定到它的對象,檢查屬性,然後如果它設置爲true暫停綁定,則將行設置爲不可見,然後恢復綁定。例如:

CurrencyManager cur = (CurrencyManager)datagrid.BindingContext[datagrid.Datasource]; 
cur.SuspendBinding(); 
datagridviewrow.Visible = false; 
cur.ResumeBinding(); 
相關問題