我在Winforms应用程序中有以下代码,它按预期工作:
private void btnFetchCollections_Click(object sender, EventArgs e)
{
try
{
var scDal = new SiteCollectionDal();
var dt = new DataTable();
List<SiteCollectionEntity> siteCollections = scDal.FetchSiteCollections();
dt.Columns.Add(new DataColumn("Site Name", typeof(string)));
foreach (SiteCollectionEntity siteCollectionEntity in siteCollections)
{
DataRow row = dt.NewRow();
row["Site Name"] = siteCollectionEntity.Url;
dt.Rows.Add(row);
dt.AcceptChanges();
}
dataSiteCollections.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
我现在正在为这个项目构建一个新的WPF接口,以下内容不起作用 - 我应该如何接近它?
private void btnFetchSiteCollections_Click(object sender, RoutedEventArgs e)
{
try
{
var scDal = new SiteCollectionDal();
var dt = new DataTable();
List<SiteCollectionEntity> siteCollections = scDal.FetchSiteCollections();
dt.Columns.Add(new DataColumn("Site Name", typeof(string)));
foreach (SiteCollectionEntity siteCollectionEntity in siteCollections)
{
DataRow row = dt.NewRow();
row["Site Name"] = siteCollectionEntity.Url;
dt.Rows.Add(row);
dt.AcceptChanges();
}
dataSiteCollections.ItemsSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
我收到的错误在以下一行:
dataSiteCollections.ItemsSource = dt;
这是:
无法将源类型'System.Data.DataTable'转换为目标类型 'System.Collections.IEnumerable'
答案 0 :(得分:1)
试试这个:
dataSiteCollections.ItemsSource = dt.AsDataView();