2011-03-16 83 views
2

我想刪除所有行來重新加載它們(我需要刪除所有行)由於某種原因,它不會從我的datagridview中刪除它們(它不添加任何額外的任何)發生。我嘗試了很多不同的方法,我知道我過去做過這些。 (也許是因爲它的一天結束)從dataGridView使用數據刪除行

這裏是我的代碼試圖一大堆消除了

 private void loadMSXGridView() 
    { 
     BindingSource bs = new BindingSource(); 

     dgv.DataSource = null; 
     dgv.Refresh(); 
     bs.DataSource = GetTable(); 
     dgv.DataSource = bs; 
     dgv.Columns[0].Width = 391; 
     dgv.Columns[1].Width = 30; 
    } 

     private DataTable GetTable() 
     { 
      DataTable t = new DataTable(); 
      t.Rows.Clear(); 
      t.AcceptChanges(); 

      t.Columns.Add("Accounting Line", typeof(string)); 
      t.Columns.Add("#", typeof(string)); 
      foreach (AMAPnr.RemarkElement re in AMAPnr._RemarkElements) 
      { 
       if (re.ElementID == "RM" && re.FreeFlow.StartsWith("*MS")) 
       { 
        DataGridViewCell gridCellText; 
        DataGridViewCell gridCellElement; 

        gridCellText = new DataGridViewTextBoxCell(); 
        gridCellText.Value = re.FreeFlow; 

        gridCellElement = new DataGridViewTextBoxCell(); 
        gridCellElement.Value = re.ElementNo.ToString(); 

        t.Rows.Add(gridCellText.Value, gridCellElement.Value); 
       } 
      } 
      return t; 
     } 

我的刪除按鈕調用loadMSXGridView,我只需要刷新一切,因爲在我的datagridview的項目相關聯以要素號,這將不會保持不變

+0

爲什麼不使用另一個BindingSource並將它關聯到網格? BindingSource bs = new BindingSource(); bs.DataSource = YourSource; dgv.DataSource = bs; – WorldIsRound 2011-03-16 22:15:14

+0

@HelloWorld更新問題,我沒有測試,因爲它需要一段時間才能編譯,只要它工作?或者我應該使用不同的方法 – Spooks 2011-03-16 22:19:42

+0

我會試一試! – WorldIsRound 2011-03-16 22:22:38

回答

0

是我的錯,一切都運行throughough調試後我發現工作 備註元素沒有被刪除,因此它通過添加相同的項目被刪除。我從RemarkElement部分刪除項目,它的工作原理,感謝您的幫助每個人!

1

最初,你是數據綁定:

dgv.DataSource = GetTable(); 

你應該再繼續數據綁定;或者告訴自行清除(並重新填充數據),或者將不同的數據表分配給dgv.DataSource

+0

我試圖將dgv.DataSource設置爲null,因爲loadMSXGridView()是選擇刪除行後調用的方法,還是應該執行完全空白的表? – Spooks 2011-03-16 22:21:23

+0

@Spooks我會做一個空的(不是我使用DataTable太多) – 2011-03-16 22:27:45

+0

我會試一試,你通常使用什麼? – Spooks 2011-03-16 22:28:55

1

在我有限的數據綁定體驗中,編輯數據源並不容易。您應該使用行綁定或其他事件之一來處理數據編輯。但是,如果您想編輯數據源,則基本上必須克隆結構,然後導入行。所以從這個意義上講,你應該回到你的數據源並選擇你想要的東西。

但是..

如果要過濾/此處修改您的數據是如何可以做到這一點:

DataSet ds1 = [Your Data Source] 
DataSet ds2 = new DataSet(); 

ds2 = ds1.Clone(); 

for (int i = 0; i < ds1.Tables[0].Rows.Count; i++) 
{ 
    if (ds1.Tables[0].Rows[i].ItemArray[0].ToString() != "SomeValue") 
    {   
     ds2.Tables[0].ImportRow(ds1.Tables[0].Rows[i]); 
    } 
} 
ds1 = ds2; 
+0

我用了一個非常相似的DataTable,但我不得不重置數據源,所以mydgv.DataSource = table2; – Rob 2015-01-09 19:12:02

1

如果你僅僅只是想添加不同的數據表作爲數據源,然後執行:

datagridview.DataSource = datatable; 
datagridview.Invalidate(); 

這應該工作,正如我剛纔做了一個項目我在工作的完全一樣的東西一分鐘:)

0

我建議設置你的DataGridView到的BindingSource的DataSource和改變的BindingSource的數據源,而不是:這解決了大多數

var table1 = GetDataTable(); 
var bindingSource1 = new BindingSource(table1, null); 
dataGridView1.DataSource = bindingSource1; 


// after reload 
var table2 = GetDataTable(); 
bindingSource1.DataSource = table2; 

問題,因爲您不必擔心數據的綁定方式。