2009-11-23 95 views
0

我用下面的代碼到一個ArrayList綁定到DataGridWPF的Datagrid刷新數據

//fill datagrid 
    public DataTable configData 
    { 
     get 
     { 
      DataSet dsTemp = new DataSet(); 
      DataTable Tables = new DataTable(); 
      dsTemp.Tables.Add(Tables); 

      dsTemp.Tables[0].Columns.Add("val", System.Type.GetType(
      "System.String")); 

      foreach (string str in IMDB.ML.Class.Config.getReadPaths()) 
      { 
       if (str != string.Empty) 
       { 
        DataRow myRow = dsTemp.Tables[0].NewRow(); 
        myRow[0] = str; 
        dsTemp.Tables[0].Rows.Add(myRow); 
       } 
      } 
      return dsTemp.Tables[0]; 
     } 
    } 

不過,我需要能夠刷新我的數據我想補充一些新的數據列表之後。

datagrid.Items.Refresh()不工作...

的感謝!

+0

又見網格將更新:http://stackoverflow.com/questions/1751897/how-to-get-the-wpf- toolkit-datagrid-to-show-new-rows-when-bound-to-dataset – 2009-11-23 17:24:41

+0

您是否正在從UI線程更新DataTable? – 2009-11-23 17:25:19

回答

2

首先,您返回的是DataTable,而不是ArrayList。 第二,如果你使用一個數據視圖當數據更新...

//fill datagrid 
    public ICollectionView configData 
    { 
     get 
     { 
      DataTable table = new DataTable(); 

      table.Columns.Add("val", typeof(string)); 

      foreach (string str in IMDB.ML.Class.Config.getReadPaths()) 
      { 
       if (!string.IsNullOrEmpty(str)) 
       { 
        DataRow myRow = table.NewRow(); 
        myRow["val"] = str; 
        table.Rows.Add(myRow); 
       } 
      } 
      return CollectionViewSource.GetDefaultView(dsTemp.Tables[0])   
     } 
    } 
+0

謝謝,問題解決後,我重新加載這個視圖作爲itemsource – WtFudgE 2009-11-24 09:38:00