【发布时间】:2015-11-26 02:07:45
【问题描述】:
基本上,我要处理 4 个文件:
DBDisplay.xaml
DBDisplay.xaml.cs
DBDisplayViewModel.cs
DBConn.cs
在我的 ViewModel 中,我试图从我的 .xaml 文件中填充以下 DataGrid:
<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 应该链接到我的数据库(因为这是我之前连接到用户列表的方式)
另外我想我需要类似的东西
DataGrid.DataSource = table; //DataGrid would be linking to the xaml code
填充数据网格
我就到这里了,如果有人能帮忙,我会很高兴的!
【问题讨论】: