使用虚拟数据创建 DataTable 对象
我正在尝试将 DataTable 数据绑定到手风琴,我发现如果我使用表适配器从数据库检索 DataTable,它会完美地绑定到手风琴,但是我想要做的是创建一个虚拟表(用于测试目的,如果我无权访问我的数据库)创建虚拟表的代码如下:
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);
当我尝试数据绑定该表时,但手风琴不显示。我可以将它绑定到网格视图或详细信息视图,但不能绑定到手风琴。
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);
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
经过 4 个小时的头撞墙后,我发现 DataSource 字段非常挑剔。
这是我的代码:
事实证明,手风琴只喜欢绑定到数据集表的默认视图。我尝试仅绑定到 DataTable (dt),但失败了。甚至 dt.DefaultView 也失败了。一旦我将它添加到数据集中,它就像冠军一样绑定。非常烦人,浪费时间。我知道您可能早已忘记了这一点,但我想让未来的搜索者可以使用它。 Accordion.DataSource 必须绑定到 DataSet.Table.DefaultView 才能工作。
After 4 hours of banging my head against the wall, I discovered that the DataSource field is VERY picky.
Here's my code:
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.
确保为 table2.Columns.Add(...) 中的列指定类型
Make sure you specify a type for the columns in the table2.Columns.Add(...)
另外,如下面的答案所示:
https://stackoverflow.com/a/6108163/637903
您可以绑定对从原始 DataTable 构造的 DataTableReader 的手风琴控制
Also, as seen in the answer below:
https://stackoverflow.com/a/6108163/637903
You can bind the Accordion Control to a DataTableReader constructed from the original DataTable