[英]Inserting value in Observable Collection From SQl Db In ViewModel Class
I have Table with two Columns: CourseID and CourseNAme.我有两列的表:CourseID 和 CourseNAME。 I want values of these two columns in Observable COllection - FillCourseId.
我想要 Observable COllection - FillCourseId 中这两列的值。 in my view model Class.
在我看来 model Class。 Please help me iam not able to populate my Observable collection with the values from my dataBase
请帮助我,我无法使用我的数据库中的值填充我的 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; }
I have Table with two Columns: CourseID and CourseNAme.我有两列的表:CourseID 和 CourseNAME。 I want values of these two columns in Observable COllection - FillCourseId.
我想要 Observable COllection - FillCourseId 中这两列的值。 in my view model Class.
在我看来 model Class。 Please help me iam not able to populate my Observable collection with the values from my dataBase
请帮助我,我无法使用我的数据库中的值填充我的 Observable 集合
add this in app.xaml.cs It will work将此添加到 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.