尝试获取源中的更改以反映在UI中(WPF DATAGRID) [英] trying to get changes in source to reflect in UI (WPF DATAGRID)

查看:91
本文介绍了尝试获取源中的更改以反映在UI中(WPF DATAGRID)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我遇到以下问题.我已经创建了一个对象,并使用MySql数据库中的值填充了该对象.每个对象都添加到一个列表(集合)中.然后,我将此对象列表数据绑定到数据网格的itemsource.我利用了一个objectdataprovider.我的问题是,如果在数据库中进行了更改,则不会更新我的收藏集,因此不会更新我的UI.关于可以做什么的任何建议.我是编程和WPF的新手,但是我没有头绪.
请帮忙.

以下是代码:



I am having the following problem. I have created an object and I populate the object with values from my MySql database. Each object in added to a list(collection). I then databind this list of objects to the itemsource of a datagrid. I make use of an objectdataprovider. My problem is that if changes are made in my database, my collection is not updated and therefore my UI isn''t updated. Any suggestions on what can be done. I new to programming and WPF and I havent got a clue.
Please help.

Following is the code:

 public class CompressorEngine : ObservableCollection <power>
    {
        public static ArrayList GetCompressor()
        {
            MySqlConnection conn = new MySqlConnection(" ");
            conn.Open();
            MySqlCommand comm = conn.CreateCommand();
            comm.CommandText = "SELECT ABIUnits.UnitSerialNumber, Region, AssetDescription, Location_Latitude, Location_Longitude, Street, City, ABIUnits_Alerts.Time, ABIUnits_Alerts.Comment, Alert_Type, Alert_ID FROM ABIUnits JOIN ABIUnits_Alerts ON ABIUnits.UnitSerialNumber = ABIUnits_Alerts.UnitSerialNumber WHERE ABIUnits_Alerts.Checked = '" + 0 + "' AND ABIUnits_Alerts.AlertGroup = '" + 3 + "' ORDER BY ABIUnits_Alerts.Time Desc";
           
            MySqlDataReader reader = comm.ExecuteReader();
            string alert_id;
            string unitserialnumber;
            string alerttype;
            string time1;
            string assetdescription;
            string region;
            string location_latitude;
            string location_longitude;
            string street;
            string city;
            ArrayList plist = new ArrayList();
            Compressor titlepower = new Compressor("Alert ID", "Unit Serial Number", "Alert", "Time", "Description", "Latitude", "Longitude", "Region", "Street", "City");
            plist.Add(titlepower);
            while (reader.Read())
            {
                alert_id = reader["ALert_ID"].ToString();
                unitserialnumber = reader["UnitSerialNumber"].ToString();
                alerttype = reader["ALert_Type"].ToString();
                time1 = reader["Time"].ToString();
                assetdescription = reader["AssetDescription"].ToString();
                region = reader["Region"].ToString();
                location_latitude = reader["Location_Latitude"].ToString();
                location_longitude = reader["Location_Longitude"].ToString();
                street = reader["Street"].ToString();
                city = reader["City"].ToString();
                Compressor compressor = new Compressor(alert_id, unitserialnumber, alerttype, time1, assetdescription, location_latitude, location_longitude, region, street, city);
                plist.Add(compressor);

            }
            conn.Close();
            return plist;
        }
    }
    public class Compressor : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string info)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(info));
            }

        }
        private string _p_id;
        private string _units;
        private string _alert;
        private string _time;
        private string _assetdescription1;
        private string _location_latitude1;
        private string _location_longitude1;
        private string _region1;
        private string _street1;
        private string _city1;

        public Compressor(string p_id, string units, string alert, string time, string assetdescription1, string location_latitude1, string location_longitude1, string region1, string street1, string city1)
        {
            _p_id = p_id;
            _units = units;
            _alert = alert;
            _time = time;
            _assetdescription1 = assetdescription1;
            _location_latitude1 = location_latitude1;
            _location_longitude1 = location_longitude1;
            _region1 = region1;
            _city1 = city1;
            _street1 = street1;

        }


        public string P_id
        {
            get { return _p_id; }
            set { _p_id = value; OnPropertyChanged("P_id"); }

        }
        public string Units
        {
            get { return _units; }
            set { _units = value; OnPropertyChanged("Units"); }
        }
        public string Alert
        {
            get { return _alert; }
            set { _alert = value; OnPropertyChanged("Alert"); }
        }

        public string Time
        {
            get { return _time; }
            set { _time = value; OnPropertyChanged("Time"); }
        }
        public string AssetDescription1
        {
            get { return _assetdescription1; }
            set { _assetdescription1 = value; OnPropertyChanged("AssetDescription1"); }
        }
        public string Location_Latitude1
        {
            get { return _location_latitude1; }
            set { _location_latitude1 = value; OnPropertyChanged("Location_Latitude1"); }
        }
        public string Location_Longitude1
        {
            get { return _location_longitude1; }
            set { _location_longitude1 = value; OnPropertyChanged("Location_Longitude1"); }
        }
        public string Region1
        {
            get { return _region1; }
            set { _region1 = value; OnPropertyChanged("Region1"); }
        }
        public string Street1
        {
            get { return _street1; }
            set { _street1 = value; OnPropertyChanged("Street1"); }
        }
        public string City1
        {
            get { return _city1; }
            set { _city1 = value; OnPropertyChanged("City1"); }
        }
    }
</power>

推荐答案

用户是否有任何事件可以作为查询检查数据库更改的好地方?是什么导致数据库更改?也许它可以向程序发送信号或发出标志以检查DB的更新?您是否考虑过使用触发器?

如果对更改的查询不太繁琐,是否可以定期检查?

我只是散发出想法,显然有些想法比其他想法更受欢迎.
Is there any event by the user that would be a good place for a query to check for changes to the DB? What causes the change to the DB? Perhaps it can send signal or raise a flag to the program to check the DB for the update? Have you looked into using Triggers?

If the query for changes is not too taxing, could you just periodically check?

I''m just shooting off ideas, obviously some are preferred over others.


一种流行的方法是,插入/更新数据库的客户端将广播数据库更新的消息,该消息表明"被所有其他活动客户端收到.该消息可以直接提供插入或更新的数据,或者客户端可以选择执行db-refresh来获取更改(这是您需要做出的设计决策).

如果您的设计不允许您进行广播,例如当客户端直接与数据库服务器(旧式客户端服务器应用程序)进行通信,而客户端无法相互通信时,则您必须诉诸轮询数据库以获取固定的更改.间隔.这容易出错,并且可能导致冲突,因此,如果您确实有选择,我强烈建议您采用上述方法或类似的方法.
One popular approach is that the client that inserts/updates the database will broadcast a db-updated message that''s received by all other active clients. The message can directly provide the inserted or updated data, or the clients can choose to do a db-refresh to get the changes (this is a design decision you need to make).

If your design will not let you do this broadcast, example when clients directly talk to a DB server (old school client-server app), and clients cannot communicate to each other, then you have to resort to polling the DB for changes at fixed intervals. This is error prone and can cause conflicts, so if you do have the option I''d strongly recommend that you take the above mentioned approach or a similar variation.


这篇关于尝试获取源中的更改以反映在UI中(WPF DATAGRID)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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