创建虚拟数据DataTable对象对象、数据、DataTable

2023-09-10 18:01:49 作者:Bleak 暗淡

我想数据绑定一个DataTable到手风琴,我发现,如果我从数据库中检索数据表使用其绑定到手风琴完美但是我想要做的是创建一个虚拟表(用于测试的表适配器如果我没有访问我的数据库)的code创建的虚拟表的目的是如下:

I am trying to databind a DataTable to an accordion and I have found that If I retrieve the DataTable from a database using a table adapter it binds to the accordion perfectly however what I want to do is create a dummy table (for testing purposes if I don't have access to my database) the code to create the dummy table is below:

    DataTable table2 = new DataTable("articletable");
    table2.Columns.Add("articleID");
    table2.Columns.Add("title");
    table2.Columns.Add("content");

    DataRow row = table2.NewRow();
    row[0] = "1";
    row[1] = "article name";
    row[2] = "article contents go here";
    table2.Rows.Add(row);

当我尝试数据表绑定然而手风琴不显示。我可以将它绑定到一个GridView或DetailsView控件,但不手风琴。

When I try to data bind that table however the accordion does not display. I can bind it to a gridview or detailsview but not the accordion.

推荐答案

4小时撞我的头靠在墙上后,我发现,数据源字段是很挑剔的。

After 4 hours of banging my head against the wall, I discovered that the DataSource field is VERY picky.

下面是我的code:

DataSet ds = new DataSet();

        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("Branch");
        dt.Columns.Add("Officer");
        dt.Columns.Add("CustAcct");
        dt.Columns.Add("Grade");
        dt.Columns.Add("Rate");
        dt.Columns.Add("OrigBal");
        dt.Columns.Add("BookBal");
        dt.Columns.Add("Available");
        dt.Columns.Add("Effective");
        dt.Columns.Add("Maturity");
        dt.Columns.Add("Collateral");
        dt.Columns.Add("LoanSource");
        dt.Columns.Add("RBCCode");

        dt.Rows.Add(new object[] { "James Bond, LLC", 120, "Garrison Neely", "123 3428749020", 35, "6.000", "$24,590", "$13,432",
            "$12,659", "12/13/21", "1/30/27", 55, "ILS", "R"});

        ds.Tables.Add(dt);

        accReportData.DataSourceID = null;
        accReportData.DataSource = ds.Tables[0].DefaultView;
        accReportData.DataBind();

原来,手风琴只是喜欢被绑定到数据集表的默认视图。我想结合只是一个数据表(DT),它失败了。即使dt.DefaultView失败。当我把它添加到DataSet,它结合如飞。很烦人,与失去的时间浪费。我知道你可能很久以前就忘了这一点,但我想让它未来的搜索提供。 Accordion.DataSource必须绑定到一个DataSet.Table.DefaultView工作。