[C #] DataTables.net: dynamic columns in number and name

[C #] DataTables.net: dynamic columns in number and name

robertino_salemirobertino_salemi Posts: 13Questions: 0Answers: 0
edited April 2014 in DataTables 1.9
Hello,
I'm using your DataTables.

Until now I used the DataTables creating a model with fixed columns of the table and then populating a DataTable with C #.

But now I need to create a dynamic header, ie with the number and name of columns that vary in construction to those present in the DataTable C #.

I can not figure it out, suggestions?

Thank you.

Replies

  • robertino_salemirobertino_salemi Posts: 13Questions: 0Answers: 0
    This is my solution:

    [code]
    public static class DataTableExtension
    {
    public static MvcHtmlString ToHTMLTableDataTable(this DataTable model, string id)
    {
    string table = @"

    ";
    foreach (System.Data.DataColumn column in model.Columns)
    {
    table += "" + column.Caption + "";
    }
    table += @"

    ";
    foreach (System.Data.DataRow row in model.Rows)
    {
    table += " ";
    foreach (System.Data.DataColumn column in model.Columns)
    {
    table += "" + row[column].ToString() + "";
    }
    table += "";
    }
    table += "";

    return new MvcHtmlString(string.Format("{1}", id, table));
    }
    [/code]
This discussion has been closed.