2011-06-29 8 views

回答

2

您應該可以通過添加BindingSource來達到您想要的效果。

bindingSource1.DataSource = yourdatasource; 
dataGridView1.DataSource = bindingSource1; 

在DataGridView的,那麼你可以更改綁定源的RaiseListChangedEvents財產CellBeginEdit和CellEndEdit事件:

void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
{ 
    bindingSource1.RaiseListChangedEvents = true; 
} 

void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) 
{ 
    bindingSource1.RaiseListChangedEvents = false; 
} 

我睡眠有後臺工作測試這裏面它通過一個按鈕啓動,並在此之後更新綁定列表。我按下按鈕,然後編輯一個單元格,並在計時器過期後,我對單元格的更改仍然保留。

當我沒有開始編輯單元格更改到列表被更改。


有一點需要注意的是,這是整個綁定源,而不是一個特定的細胞。

+0

謝謝!那是我需要的。 – Peter17

1

如果你總是希望bound屬性只在離開字段時更新(或者當字段從代碼更新時),那麼我認爲你應該使用DataBindingMode.OnValidated而不是DataBindingMode.OnPropertyChanged當你創建你的綁定。

+0

DataGridView沒有DataBindingmode屬性。我怎樣才能改變這種模式? – Peter17

+0

myDataGridView.DataBindings.Add具有DataSourceUpdateMode類型的參數。 –

相關問題