我已經綁定我DataGridView
到BindingSource
,但是當我打電話ResetBinding()
的DataGridView
沒有更新,只顯示那些已WhenDeleted列值作爲空記錄:BindingSource.BindingReset不工作
public partial class LabourSetup : DisplayControl
{
private List<LABOUR> labourCollection = new List<LABOUR>();
protected BindingSource bs = new BindingSource();
private void PopulateGrid()
{
labourCollection = Query.GetLabours();
bs.DataSource = labourCollection;
this.dataGridViewX1.DataSource = bs;
}
// This method doesn't actually delete Labour from table just mark the column with the present day date. So that it could not be shown in Grid View
public void btnDel_Click(object sender, EventArgs e)
{
if(dataGridViewX1.SelectedRows.Count == 1)
{
dataGridViewX1.SelectedRows[0].Cells["WhenDeleted"].Value = DateTime.Now;
}
MainForm.GlobalCache.SaveChanges();
labourCollection = Query.GetLabours();
//bs.DataSource = labourCollection; // works when I un comment this line
bs.ResetBindings(false); // doesnt work
}
}
}
在上面,爲什麼我必須寫:
//bs.DataSource = labourCollection; // works when I un comment this line. Why?
由於綁定源已綁定到PopulateGrid()方法中的labourCollection。 ResetBinding()不應顯示標記爲刪除的記錄(WhenDeleted不爲空)。
public class Query
{
internal static List<LABOUR> GetLabours()
{
// Getting only those records which are not marked for deletion
return MainForm.GlobalCache.LABOURs.Local.Where(lab => lab.WhenDeleted == null).ToList();
}
}
以上GlobalCache
是DbContext
。 幫助表示讚賞。
謝謝Reza的詳細解釋。我試着BindingSource的Filter屬性只包含那些WhereDeleted爲空的記錄。但後來它說我的列表(本地在我的情況下)應該實現IBindingListView所需的結果。不知道如何實現,沒有bs.DataSource = labourCollection;在btnDel_Click事件 – Sadiq
不客氣。設置屬性值後,可以從'labourCollection'中刪除該項目。別擔心,它不會從上下文中刪除。它只會從'labourCollection'中刪除。 –
再次設置數據源不是什麼大問題,這有什麼問題? –