Observable Collection不在运行时从SQL Server数据库更新

时间:2016-02-17 06:44:19

标签: c# sql-server wpf data-binding observablecollection

我已经实现了INotifyPropertyChanged和Observable Collection,但是我在数据库中所做的更改没有反映在运行时的DataGrid上(UI没有更新)。我是c#和WPF的新手。这是我的代码。

模型和视图模型:

namespace WpfApplication4.ViewModels
{
    public class MainViewModel : INotifyPropertyChanged
    {
        Model _myModel = new Model();
        private ObservableCollection<VItalView> _vitalview = new ObservableCollection<VItalView>();
        public ObservableCollection<VItalView> Vitalview
        {
            get { return _vitalview; }
            set
            {
                _vitalview = value;
                OnPropertyChanged("Vitalview");
            }
        }
        public MainViewModel()
        {
            initializeload();
        }
        private void initializeload()
        {
            DataTable table = _myModel.getData();

            for (int i = 0; i < table.Rows.Count; ++i)
                Vitalview.Add(new VItalView
                {
                    Weight = Convert.ToInt32(table.Rows[i][0]),
                    Height = Convert.ToInt32(table.Rows[i][1]),
                    BMI = Convert.ToInt32(table.Rows[i][2]),
                });
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyname)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyname));
        }
    }

    public class Model
    {
        public DataTable getData()
        {
            DataTable ndt = new DataTable();
            SqlConnection con = new SqlConnection("Data Source=DESKTOP-QTFGN00; Initial Catalog=VITALS;Integrated Security=true");

            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM Vitals", con);


            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.SelectCommand = cmd;

            da.Fill(ndt);
            return ndt;
        }
    }
}

观点:

namespace WpfApplication4.Models
{
    public class VItalView : INotifyPropertyChanged, IDataErrorInfo
    {
        public VItalView()
        {
        }

        private float weight;

        public float Weight
        {
            get { return weight; }
            set
            {
                weight = value;
                OnPropertyChanged("Weight");
            }
        }

        private float height ;

        public float Height
        {
            get { return height; }
            set
            {
                height = value;
                OnPropertyChanged("Height");
            }
        }

        private float bmi;

        public float BMI
        {
            get { return bmi; }
            set
            {
                bmi = value;
                OnPropertyChanged("Bmi");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private VItalView vv;

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

        public string Error
        {
            get { return null; }
        }

        public string this[string columnName]
        {
            get
            {
                string error = null;
                switch (columnName)
                {
                    case "Weight":
                        if (weight<10)
                        {
                            error = "Fdddd";
                        }
                        break;

                    case "Height":
                        if ((height < 18) || (height > 85))
                        {
                            error = "bhhj.";
                        }
                        break;
                    case "BMI":
                        if (bmi>70)
                        {
                            error = "xxx";
                        }
                        break;
                }
                return (error);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这不会像这样工作。实际上ObservableCollection<T>将适用于将修改此属性的情况。

SQL Server没有通知应用程序已经添加了任何内容。