Creating a DataTable object with dummy data
Asked Answered
S

3

10

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.

Scribner answered 29/10, 2009 at 17:14 Comment(2)
can you try to add more information to the columns such as dTable.Columns.Add("firstName", Type.GetType("System.String"))?Yankee
I could try that but as I said before I can databind that table to a gridview with no issues.Scribner
M
26

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

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", "$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();

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.

Mojica answered 26/7, 2010 at 13:55 Comment(0)
I
1

Make sure you specify a type for the columns in the table2.Columns.Add(...)

Implode answered 30/10, 2009 at 0:30 Comment(0)
V
0

Also, as seen in the answer below:

https://mcmap.net/q/1161458/-ajaxcontroltoolkit-accordion-and-databinding

You can bind the Accordion Control to a DataTableReader constructed from the original DataTable

accReportData.DataSource = new System.Data.DataTableReader(ds.Tables[0]);
accReportData.DataBind();
Vernon answered 21/6, 2013 at 21:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.