C# 使用虚拟数据创建 DataTable 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1645247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a DataTable object with dummy data
提问by Morgeh
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.
当我尝试对该表进行数据绑定时,手风琴不显示。我可以将它绑定到 gridview 或 detailsview 但不能绑定到手风琴。
采纳答案by Garrison Neely
After 4 hours of banging my head against the wall, I discovered that the DataSource field is VERY picky.
用头撞墙 4 小时后,我发现 DataSource 字段非常挑剔。
Here's my 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", ",590", ",432",
",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();
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 (dt) 并且失败了。甚至 dt.DefaultView 也失败了。一旦我将它添加到 DataSet,它就会像冠军一样绑定。非常烦人,浪费时间。我知道您可能早就忘记了这一点,但我想让未来的搜索者可以使用它。 Accordion.DataSource 必须绑定到 DataSet.Table.DefaultView 才能工作。
回答by Nestor
Make sure you specify a type for the columns in the table2.Columns.Add(...)
确保为 table2.Columns.Add(...) 中的列指定类型
回答by Pythonian
Also, as seen in the answer below:
此外,如下面的答案所示:
https://stackoverflow.com/a/6108163/637903
https://stackoverflow.com/a/6108163/637903
You can bind the Accordion Control to a DataTableReader constructed from the original DataTable
您可以将 Accordion Control 绑定到从原始 DataTable 构造的 DataTableReader
accReportData.DataSource = new System.Data.DataTableReader(ds.Tables[0]);
accReportData.DataBind();