0

我在 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”

4

1 回答 1

1

尝试这个:

dataSiteCollections.ItemsSource = dt.AsDataView();
于 2013-10-08T04:13:20.737 回答