C# 在键关闭WPF时向DataGrid添加项,性能差?

C# 在键关闭WPF时向DataGrid添加项,性能差?,c#,wpf,performance,datagrid,C#,Wpf,Performance,Datagrid,我的应用程序是这样的:我在主窗口上获得了包含产品的sql数据库。xaml有DataGrid和TextBox 应用程序非常简单(解释如下): 用户应通过在文本框中输入产品代码并按enter键来查找产品,如果数据库中存在带有该代码的产品,则该产品将添加到数据网格!之后,用户可以决定是从datagrid中删除一些项目,还是全部打印等等 下面是它的外观,我用油漆画了一点:) 我的问题从这里开始: 当我将项目从数据库添加到我的DataGrid时,它的工作速度有点慢,我的意思是它没有达到应有的速度,当项目

我的应用程序是这样的:我在主
窗口上获得了包含产品的sql数据库。xaml
DataGrid
TextBox

应用程序非常简单(解释如下):

用户应通过在
文本框中输入产品代码并按enter键来查找产品,如果数据库中存在带有该代码的产品,则该产品将添加到
数据网格
!之后,用户可以决定是从datagrid中删除一些项目,还是全部打印等等

下面是它的外观,我用油漆画了一点:)

我的问题从这里开始:

当我将项目从数据库添加到我的
DataGrid
时,它的工作速度有点慢,我的意思是它没有达到应有的速度,当项目被添加到
DataGrid
时,确实有可能感觉到有一点延迟

我以为我做了所有应该做的事情,但可能我没有,我猜一定是性能有问题,或者我的代码不好

现在我将发布我的代码,所以如果你们能给我建议如何加快它,或者建议我可以做得更好,请这样做,我真的很感激,因为我忘记了我做错了,也许我没有像它应该做的那样将数据绑定到
DataGrid
,或者其他什么,但我不希望我的应用程序太慢,特别是因为它真的很简单

这是我的密码:

//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();
                    }
                }
            }
        }
所以我想知道这段代码有什么问题,为什么我在向我的DataGrid添加项目时会感到延迟,我认为在数据库中搜索产品需要时间,但实际上我删除了除5个产品以外的所有产品,所以它应该工作得非常快,事实上这是一个非常简单的操作:

在数据库中查找项,将其插入临时表,将其添加到数据网格中

但不知怎么的,它并没有像预期的那么快,
不管怎样,谢谢大家,干杯

首先,即使在完美的实现中,数据网格也是一个沉重而缓慢的控件,因为它有很多事情要做,但是,您的问题是不断地删除和添加所有元素,这会导致从一开始就进行渲染

您应该做的是使用WPF
ObservableCollection
保存您的列表。可观察集合具有以下属性:当您向其添加或删除元素时,所有绑定将自动得到通知,UI将得到更新(只更新需要更新的内容)

在一种更好的方法中,我只在预定义的时间间隔(如每秒)将项目添加到UI集合中。如果用户连续按下按钮10次,您的UI只会使用所有新信息更新一次

要执行此操作,请首先定义一个列表:

var result = new ObservableCollection<ProductTemporary>();
最后,当您想要更改它时,只需更改
结果

result.Add(lastInserted);
最后一个提示是在另一个线程中执行非UI作业,以保持UI的响应性


希望有帮助:)

关于性能的问题很好,我希望有人能回答如何提高性能,因为我对此也很好奇<代码>dtgProductItems.ItemsSource=null为什么要这样做?@Emad因为当用户向数据网格添加项时,我想立即向他显示它,就像刷新数据网格一样。。将source设置为null,然后再次添加它,但其中包含新项..这样做似乎更快,但如何将ProductTemporary加载到ObservableCollection?因为万一计算机以某种方式关闭,当用户打开计算机并登录时,我需要将我的项目保留在那里,所以我需要这样的东西:ObservableCollection result=ProductsTempController.Instance.SelectAll();但在此之后,我面临一个错误:/n您应该从列表中创建ObservableCollection,如下所示:
new ObservableCollection(ProductsTempController.Instance.SelectAll())啊哈,我明白了,当我从datagrid中删除项时,我应该调用result.Remove(lastInserted)
ObservableCollection
ICollection
的一种实现,这意味着它的工作原理与
List
检查我的新问题一样
result.Add(lastInserted);