使用虚拟数据创建 DataTable 对象 [英] Creating a DataTable object with dummy data

查看:22
本文介绍了使用虚拟数据创建 DataTable 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 DataTable 数据绑定到手风琴,我发现如果我使用表适配器从数据库中检索 DataTable,它会完美地绑定到手风琴,但是我想做的是创建一个虚拟表(用于测试目的,如果我无权访问我的数据库)创建虚拟表的代码如下:

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 小时后,我发现 DataSource 字段非常挑剔.

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

这是我的代码:

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

事实证明,手风琴只喜欢绑定到数据集表的默认视图.我尝试只绑定到一个 DataTable (dt) 并且失败了.甚至 dt.DefaultView 也失败了.一旦我将它添加到数据集,它就会像冠军一样绑定.非常烦人,浪费时间.我知道您可能早就忘记了这一点,但我想让未来的搜索者可以使用它.Accordion.DataSource 必须绑定到 DataSet.Table.DefaultView 才能工作.

Turns out that the accordion only likes being bound to a dataset table's defaultview. I tried binding to just a DataTable (dt) and it failed. Even dt.DefaultView failed. Once I added it to a DataSet, it binds like a champ. Very annoying, with lost of wasted time. I know you've probably long-since forgotten this, but I wanted to make it available to future searchers. Accordion.DataSource must be bound to a DataSet.Table.DefaultView to work.

这篇关于使用虚拟数据创建 DataTable 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆