2017-03-02 66 views
-1

我的應用程序是關於這個的:我得到了sql數據庫裏面的產品,主要有Window.xamlDataGridTextBox添加項目到DataGrid鍵入WPF,表現不佳?

應用程序非常簡單(下面解釋):

用戶應該尋找產品在TextBox輸入產品代碼並按下Enter鍵,如果在數據庫中存在的產品與該代碼,它會被添加到DataGrid!而這之後用戶可以決定他是否打算從DataGrid中刪除某些項目,或將全部打印出來等。

這裏是如何看,我做了油漆:)

enter image description here

點點繪製

我的問題從這裏開始:

當我加入了從數據庫項目,我的DataGrid它的工作在某種程度上慢,我的意思是不是很快,因爲它應該是,它真的可以感覺到有延時,當項目的點點正在被修改爲DataGrid

我以爲我做了所有事情,但可能我沒有,我想這肯定會有一些問題,或者我的代碼不好!

現在我會發布我的代碼,所以如果你們可以給我建議如何加快它或者建議我什麼我可以做得更好,請做到這一點,我真的apparaciate,因爲不知不覺我做錯了,也許我不是將數據綁定到一個DataGrid因爲它應該還是別的什麼,但我不希望我的applicataion是緩慢的,特別是因爲它是非常簡單的應用程序..

這裏是我的代碼:

//I need to add new item from database to my DataGrid when user press Enter 
private void txtCode_KeyDown(object sender, KeyEventArgs e) 
     { 

      if (e.Key == Key.Return) 
      { 
     //Here I am making sure that I will not look for empty string in my database 
       if (!String.IsNullOrEmpty(txtCode.Text.Trim())) 
       { 
      //Here I am looking for product in my database with code that user entered 
        Article product = ArticlesController.GetProductByProductCode(txtCode.Text.Trim()); 

        if (product!= null) 
        { 
         ProductTemporary tempProduct= new ProductTemporary(); 

         tempProduct.ArticleCode = product.ArticleCode; 
         tempProduct.Price = product.Price; 
         tempProduct.Quantity = 1; 
         tempProduct.ArticleId = product.Id; 
         tempProduct.ArticleTitle = product.Title; 
         tempProduct.TotalAmount = (tempProduct.Quantity * tempProduct.Price); 

      //Here I'm adding item from database to Temp Table in case computer turns off, so when user log in back he can still find items he searched for before 
         var lastInserted = ProductsTempController.InsertNewTempProduct(tempProduct); 

         currentlyDataGridItems.Add(lastInserted); 

         dtgProductItems.ItemsSource = null; 
         dtgProductItems.ItemsSource = currentlyDataGridItems; 

         txtCode.Text = ""; 
         txtCode.Focus(); 

        } 
        else 
        { 
         MessageBox.Show("Product with next code:" + txtCode.Text + " does not exist.", "Search by product code", MessageBoxButton.OK, MessageBoxImage.Information); 
         txtCode.Text = ""; 
         txtCode.Focus(); 
        } 
       } 
      } 
     } 

所以我想知道這段代碼有什麼問題,爲什麼我必須感覺到d當我添加項目到我的DataGrid時,我相信它需要時間來搜索數據庫中的產品,但acctualy我刪除所有除了5個產品,所以它應該工作真的很快,其實這是非常簡單的行動:

在數據庫中找不到項目,將其插入到一個臨時表,將其添加到DataGrid

但不知何故,它不能按預期工作速度快, 什麼的,非常感謝球員和歡呼聲!

+0

關於性能的好問題,我希望有人會回答它如何改善,因爲我也很好奇! – 2017-03-02 21:05:42

+0

'dtgProductItems.ItemsSource = null;'你爲什麼這樣做? – Emad

+0

@Emad,因爲當用戶添加項目到一個數據網格我想立即向他展示它,像刷新數據網格..設置源爲空,然後再次添加它,但與內部的新項目.. –

回答

2

首先,即使在完美的實現中,數據網格也是沉重且緩慢的控制,因爲它有很多事情要做,但是,您的問題會不斷移除並再次添加所有元素,這會導致渲染從頭開始。

你應該做的是使用WPF ObservableCollection<T>來保存你的列表。 Observable集合具有這個屬性,當你向它們添加或刪除一個元素時,所有綁定將自動得到通知,並且UI得到更新(只需要更新)。

在一個更好的方法中,如果用戶每次按下按鈕10次,就會以每秒一次的預定義間隔向UI集合中添加項目,而您的UI只會使用全部新信息更新一次。

要做到這一點首先定義一個列表:

var result = new ObservableCollection<ProductTemporary>(); 

然後設置列表是項目的來源爲網格

dtgProductItems.ItemsSource = result; 

最後,當你想改變它只是改變了result

result.Add(lastInserted); 

對於你的最後提示是做另一個線程中的非UI作業保持UI響應。

希望它有幫助:)

+0

似乎是它做得更快後在這種方式,但我怎麼能加載我的ProductTemporary到我的ObservableCollection?因爲在某種程度上計算機關閉的情況下,當用戶打開計算機並登錄時,我需要保留我的項目,所以我需要如下所示:ObservableCollection result = ProductsTempController.Instance.SelectAll();但在此之後,我正面臨一個錯誤:/ –

+1

你應該從你的列表中創建ObservableCollection,如下所示:'新的ObservableCollection (ProductsTempController.Instance.SelectAll());' – Emad

+0

啊哈,我明白了,那該怎麼辦我從數據網格刪除項目,比我應該調用result.Remove(lastInserted); ? –