簡體   English   中英

在 ViewModel Class 中的 SQl Db 的可觀察集合中插入值

[英]Inserting value in Observable Collection From SQl Db In ViewModel Class

我有兩列的表:CourseID 和 CourseNAME。 我想要 Observable COllection - FillCourseId 中這兩列的值。 在我看來 model Class。 請幫助我,我無法使用我的數據庫中的值填充我的 Observable 集合

//     ViewMOdel Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;


namespace MVVMDemo
{
    public class ViewModel : ViewModelBase, INotifyPropertyChanged
    {
        private Student _student;
        private ObservableCollection<Student> _students;
        private ICommand _SubmitCommand;
        private ObservableCollection<Student> _fillCourseId = new 
        ObservableCollection<Student>();
        static String connectionString = @"Data Source=Ramco- 
         PC\SQLEXPRESS;Initial Catalog=SIT_Ramco_DB;Integrated 
         Security=True;";
        SqlConnection con;
        SqlCommand cmd;
       // SqlDataAdapter adapter;
       // DataSet ds;
        //SqlDataReader reader;

        public ObservableCollection<Student> FillCourseId
        {
            get { return _fillCourseId; }
            set
            {
                _fillCourseId = value;
                OnPropertyChanged("SystemStatusData");
            }
        }

        public Student Student
        {
            get
            {
                return _student;
            }
            set
            {
                _student = value;
                NotifyPropertyChanged("Student");
            }
        }
        public ObservableCollection<Student> Students
        {
            get
            {
                return _students;
            }
            set
            {
                _students = value;
                NotifyPropertyChanged("Students");
            }
        }

        public ICommand SubmitCommand
        {
            get
            {
                if (_SubmitCommand == null)
                {
                    _SubmitCommand = new RelayCommand(param => this.Submit(),
                        null);
                }
                return _SubmitCommand;
            }
        }


        public void GetCourseIdFromDB()
        {
            try
            {
                con = new SqlConnection(connectionString);
                con.Open();
                cmd = new SqlCommand("select * from dev_Course", con);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
               // Student Student = new Student();

                for (int i = 0; i < dt.Rows.Count; ++i)
                    FillCourseId.Add(new Student
                    {
                        CourseID = dt.Rows[i][0].ToString(),
                        CourseName =dt.Rows[i][1].ToString()
                    });

            }
            catch (Exception ex)
            {

            }
        }
        public ViewModel()
        {
            Student = new Student();
            Students = new ObservableCollection<Student>();
            Students.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Students_CollectionChanged);

        }

        void Students_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            NotifyPropertyChanged("Students");
        }

        private void Submit()
        {
            Student.JoiningDate = DateTime.Today.Date;
            Students.Add(Student);
            Student = new Student();
        }
        // Property Changed Event 
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyname)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyname));
        }
    }
}

//Model Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MVVMDemo
{
      public class Student
      {
          public string Name { get; set; }
          public int Age { get; set; }
          public string Course { get; set; }
          public string CourseID { get; set; }

我有兩列的表:CourseID 和 CourseNAME。 我想要 Observable COllection - FillCourseId 中這兩列的值。 在我看來 model Class。 請幫助我,我無法使用我的數據庫中的值填充我的 Observable 集合

將此添加到 app.xaml.cs 中它會起作用

public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            var mainWindow = new Window();
            var viewModel = new ViewModel();
            mainWindow.DataContext = viewModel;
            mainWindow.Show();
            viewModel.GetCourseIdFromDB();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM