通过数据库使用ViewModel填充DataGrid [英] Populate a DataGrid using ViewModel via a database
问题描述
基本上,我要处理的文件有4个:
Basically, I have a 4 files that I'm dealing with:
DBDisplay.xaml
DBDisplay.xaml.cs
DBDisplayViewModel.cs
DBConn.cs
在ViewModel中,我试图从.xaml文件填充以下DataGrid:
In my ViewModel I am trying to populate the following DataGrid from my .xaml file:
<DataGrid ItemsSource="{Binding Path=Users}"/>
使用以下代码:
public class DBDisplayViewModel
{
public ICollectionView Users { get; set; }
DBConn dbCon; // the connection object
DataSet dataSet;
DataRow dataRow;
private void Load()
{
string connectionString = Properties.Settings.Default.UserDB;
dbCon = new DBConn(connectionString);
dbCon.openConnection();
dataSet = dbCon.getDataSet(Queries.SelectAll);
DataTable table = dataSet.Tables[0];
PopulateTextFields(table, 1);
//Something to go here to populate the DataGrid
}
private void PopulateTextFields(DataTable table, int i)
{
dataRow = table.Rows[i];
}
public DBDisplayViewModel()
{
Load();
Users = CollectionViewSource.GetDefaultView(SOMETHING_HERE);
}
private void Closed(object sender, EventArgs e)
{
dbCon.closeConnection();
}
}
因此 SOMETHING_HERE
应该链接到我的数据库(因为这是我之前连接到用户列表的方式)我也在想我需要
So SOMETHING_HERE
should be linking to my database (as this is how I connected to a list of users before)
Also I'm thinking I need something like
DataGrid.DataSource = table; //DataGrid would be linking to the xaml code
要填充数据网格
我在这里并在这里结束,所以如果有人可以提供帮助,我将非常高兴!
I'm at and ends here, so if anyone can help, I'd be very happy!
推荐答案
恐怕您不会采用MVVM方式.我将用简单的术语进行解释.理想情况下,您应该有一个模型类,并且数据访问代码应返回此类对象的集合.更重要的是,您的视图模型具有多个不应承担的职责(从SOLID原理中读取S).它应该负责更改UI状态和/或在View上显示数据.应该有一个单独的类,该类将从数据库中获取数据到ViewModel中.
I am afraid you are not going the MVVM way. I will explain in simple terms. Ideally you should have a model class and collection of this class objects should be returned by your data access code. More importantly your view model has multiple responsibilities which it should not (read S from SOLID principles). It should be responsible for changing UI state and/or displaying data on View. There should be a separate class which will fetch data from database into ViewModel.
DBDisplay.xaml.cs
DBDisplay.xaml.cs
public DBDisplay()
{
InitializeComponent();
var viewModel = new DBDisplayViewModel();
viewModel.UserRepository = new UserRepository(); // You could use dependency injection but I left for simplicity.
this.DataContext = viewModel;
}
DBDisplayViewModel.cs
DBDisplayViewModel.cs
public class DBDisplayViewModel
{
private ObservableCollection<User> users;
public DBDisplayViewModel() {
Load();
}
public IUserRepository UserRepository
{
get; set;
}
public ObservableCollection<User> Users
{
get {
if(users == null) {
users = new ObservableCollection<User>();
}
return users;
}
set {
if(value != null) {
users = value;
}
}
}
private void Load() {
List<User> users = UserRepository.GetUsers();
Users = new ObservableCollection<User>(users);
}
}
IUserRepository.cs
IUserRepository.cs
public interface IUserRepository
{
List<User> GetUsers();
}
UserRepository.cs
UserRepository.cs
public class UserRepository : IUserRepository
{
public List<User> GetUsers() {
List<User> users;
// put your data access code here
// and transform list of user model using dataset or SQL data reader.
return users;
}
}
User.cs(这是模型)
User.cs (this is model)
public class User
{
// some properties
}
这篇关于通过数据库使用ViewModel填充DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!