2017-06-29 66 views
0

如果我加載了從一個數據源一個DataGridView,編程方式修改細胞在過濾的datagridview

this.exampleTableAdapter.Fill(this.refdataDataSet.Example); 

通過綁定源應用濾鏡

exampleBindingSource.Filter = "TrackingId = 'x'"; 

,然後試圖遍歷過濾的DataGridView並修改它們

foreach (DataGridViewRow row in datagridExampleList.Rows) 
{ 
    //Tried to set rows directly 
    row.Cells["TrackingId"].Value = ""; 
    //Also tried variations of setting the DataBoundItem directly 
} 

但是,它不會在整個收集FO遍歷有一些原因。

當我調試時,我得到了返回的Rows集合(用與過濾器匹配的x項過濾),但是當我更改集合中的值(特別是過濾器的目標值)時,即使過濾器暫停並且列表更改事件已設置爲false,迭代也會漏掉項目。

似乎沒有任何工作..

有什麼建議嗎?

+0

後設置過濾標準數據視圖會隱藏不符合標準的那些行,他們就成爲了迭代無形。但是,如果您需要迭代所有行,則可以在迭代之前將Filter屬性設置爲null,然後在完成迭代後將其恢復。但是你說你正在迭代你的DataGridView的行。請提供您的代碼,以便我們可以重現此問題。 – Bahrom

+0

謝謝Bahrom(戒指之王?) - 我在我的問題中添加了更多信息,基本上我創建了一個最小的項目,並且可以在各種情況下重複(不願意說錯誤)問題多次。基本上,Winforms,任何數據源,將datagridview拖放到表面上,一個按鈕將數據過濾到任何你喜歡的位置,另一個按鈕用於重置已過濾的項目。< - Thius是問題所在。迭代和更改數據。 。瞧...不是所有的行都可以改變 –

回答

1

以下代碼必須讓您更改所有行。

  var filter = exampleBindingSource.Filter; 
      exampleBindingSource.Filter = null; 
      foreach (DataGridViewRow row in datagridExampleList.Rows) 
      { 
       //Tried to set rows directly 
       row.Cells["TrackingId"].Value = ""; 
       //Also tried variations of setting the DataBoundItem directly 
      } 
      exampleBindingSource.Filter = filter; 
+0

工作,謝謝..最後很簡單:) –

+0

不客氣。 – Bahrom

0

爲了避免行枚舉,你可以嘗試循環,而不是:

int c = datagridExampleList.Columns["TrackingId"].Index; 

for (int r = datagridExampleList.RowCount - 1; r >= 0; r--) 
    datagridExampleList[c, r].Value = "";