Populate a DataGrid using ViewModel via a database

被刻印的时光 ゝ 提交于 2021-01-27 12:51:01

问题


Basically, I have a 4 files that I'm dealing with:

DBDisplay.xaml

DBDisplay.xaml.cs

DBDisplayViewModel.cs

DBConn.cs

In my ViewModel I am trying to populate the following DataGrid from my .xaml file:

<DataGrid ItemsSource="{Binding Path=Users}"/>

With the following code:

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();
    }

}

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

To populate the DataGrid

I'm at and ends here, so if anyone can help, I'd be very happy!


回答1:


As you are a newcomer to WPF, so I will keep things simple. To show a list of records, you need a collection. This collection you can get in your code using something like :

Users = CollectionViewSource.GetDefaultView(dataset1.Tables[0].DefaultView);




回答2:


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

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

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

public interface IUserRepository
{
   List<User> GetUsers();
}

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 (this is model)

public class User
{
    // some properties
}


来源:https://stackoverflow.com/questions/33929513/populate-a-datagrid-using-viewmodel-via-a-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!