问题我无法刷新datagrid wpf中的数据 [英] Problem I cannot refresh datas in datagrid wpf

查看:93
本文介绍了问题我无法刷新datagrid wpf中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主要形式的datagrid,它从databse Firebird中获取数据.
我以另一种形式通过wcf服务编辑数据库中的数据,其中描述了一种创建,更新我的数据的方法.

但是当我在第二个窗口中创建新记录时,我无法刷新datagrid中的数据. 使用类从数据库中获取数据到datagrid.

按钮的编辑,新建和删除的代码..

I have a datagrid in main form, which take datas from databse Firebird.
In another form i''am editing datas in database by wcf service,in which i describe a methods to create,update my datas.

But I cannot refresh datas in datagrid when i create a new record in second window..
Datas to datagrid i''am taking from database by using a class.

code of button edit,new and delete ..

private void QueueOperations(int tag)
        {
            Queue q = new Queue();
            MainWindow mn=new MainWindow();
            ISDElectronicQueueWcfServiceLibrary.Queue queue = new ISDElectronicQueueWcfServiceLibrary.Queue();
            
            switch (tag)
            {
                case 1:
                    {
                        if (ttbQueueName.Text == "")
                        {
                            MessageBox.Show("Заполните наименование очереди", this.Title);
                            ttbQueueName.Focus();
                        }
                        else
                        {
                            try
                            {
                                svc.CreateQueue(ttbQueueName.Text, ttbQueueDescription.Text);
                                ttbQueueDescription.Text = "";
                                ttbQueueName.Text = "";
                                q.dtgQueue.ItemsSource = svc.GetQueuefromClass(0, 0);
                                q.dtgQueue.Items.Refresh();
                                Close();
                            }
                            catch (Exception str)
                            {
                                MessageBox.Show("Такая очередь уже существует\n" + str.Message, this.Title);
                            }
                        }
                    }
                    break;
                case 2:
                    {
                        if (ttbQueueName.Text.Length == 0)
                        {
                            MessageBox.Show("Нужно заполнить наименование", mn.Title);
                            ttbQueueName.Focus();
                        }
                        else 
                        {
                            try
                            {
                                queue.id = queueid;
                                queue.name = ttbQueueName.Text;
                                queue.description = ttbQueueDescription.Text;
                                svc.SetQueue(queue);                               
                                ttbQueueName.Text = "";
                                ttbQueueDescription.Text = "";
                                q.dtgQueue.ItemsSource = svc.GetQueuefromClass(0, 0);
                                q.dtgQueue.Items.Refresh();
                                Close();
                            }
                            catch (Exception e)
                            {
                               MessageBox.Show(e.Message, mn.Title);
                             }  
                        }
                    }
                    break;
                case 3:
                    {
                        svc.ClearQueue(queueid);
                        ttbQueueName.Text = "";
                        ttbQueueDescription.Text = "";
                        q.dtgQueue.ItemsSource = svc.GetQueuefromClass(0, 0);
                        q.dtgQueue.Items.Refresh();
                        Close();
                    }
                    break;
            }
            ttbQueueName.Focus();
            
        }

推荐答案

对数据使用ObservableCollection( ^ ]).添加或删除项目时,ObservableCollection处理NotifyPropertyChanged.

由于仅看到解决方案的一部分,因此不确定所有内容.您可能需要提供一个适配器,以将您的Queue(?猜您正在使用队列)转换为ObservableCollection.您还可以创建一个控件,该控件执行从ObservableCollection继承的Queue操作所需的操作
Use an ObservableCollection for the data (http://msdn.microsoft.com/en-us/library/ms668604.aspx[^]). The ObservableCollection handles the NotifyPropertyChanged when items are added or deleted.

Since only see part of the solution, so not sure about everything. You may need to provide an adapter to convert your Queue (? guess you are using a queue) to an ObservableCollection. You could also create a control that does what you want as far as Queue operations the inherits from ObservableCollection


尝试此操作..
dataGrid1.BeginEdit();
////////////在这里添加您的代码////////////
dataGrid1.CommitEdit();//或仅使用此
然后刷新datagrid
try this..
dataGrid1.BeginEdit();
///////////add your code here/////////////
dataGrid1.CommitEdit();//or use only this
then refresh datagrid


这篇关于问题我无法刷新datagrid wpf中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆