2016-02-17 39 views
0

我已經實現了INotifyPropertyChanged和Observable集合,但我在數據庫中所做的更改未在運行時的DataGrid(UI未更新)中反映出來。林新的C#和WPF。這是我的代碼。可觀察集合在運行時不從SQL Server數據庫更新

模型和視圖模型:

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); 
      } 
     } 
    } 
} 
+0

https://msdn.microsoft.com/en-us/library/62xk7953%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 –

+0

你真的認爲 「SELECT * FROM生命體徵」 是動態的? – Paparazzi

+0

有其他選擇嗎?即使我更新顯示字段中的值,UI也不會更新。我如何去做這件事? – Reems9

回答

0

嗯,這不會喜歡這個工作。其實ObservableCollection<T>將在該屬性將被修改的情況下工作。

SQL Server不通知應用程序在那裏添加了任何東西。

+0

那麼如何實現這一功能? – Reems9

+0

https://msdn.microsoft.com/en-us/library/62xk7953%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 正如您在帖子中的評論所述。 – MadOX