none
Conversion of Dynamic DataTable into Generic List

    Question

  • Hi,

    I need to convert a datatable with dynamically generated columns into a generic list for WCF service method. So that i can bind that list to a datagridview or gridview in asp.net.

    Gridview should be able to display rows and columns both. Any quick idea would be really helpful. :)

    Thanks in advance to all.

    Thursday, June 09, 2011 4:41 PM

Answers

  •  

    Hi,

     

    A general way to achieve your purpose as below:

     

    private List<String> DataTableToList(DataTable dt)
            {
                List<
    string> str = new List<string>();
               
    foreach (DataRow row in dt.Rows)
                {
                   
    foreach (DataColumn col in dt.Columns)
                    {
                        str.Add(row[col].ToString());
                    }

                }

                return str;
            }

    Moreover, we can use LinQ technology to do that, you can post thread in ADO.NET DataSet forum to get tech-support.

     

    Have a nice day!

    Sincerely,


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Paul Zhou Friday, June 17, 2011 5:35 AM
    Monday, June 13, 2011 8:04 AM

All replies

  • It would help to understand why using the DataTable won't work for you.
    Thursday, June 09, 2011 8:03 PM
  • Hi:

    In general WCF should have a data contract.  This is an interface declaration with special attributes applied.  This contract does not lend itself to dynamic fields.  Are you converting rows to columns (pivot table)?

    Phil


    PHuhn
    Thursday, June 09, 2011 8:29 PM
  • Hi,

    Yes i am using Pivot table and couldn't create Data Contract for dynamic column generated table.

    Friday, June 10, 2011 6:20 AM
  • Hi:

    I believe you need to handle the pivot on the client side.  That is how I am currently handling pivot tables. It also allows one to give the user the raw data.

    Telerik has an example:

    http://demos.telerik.com/aspnet-ajax/grid/examples/programming/pivot/defaultcs.aspx

    You do not have to use their grid, but the concept is in the example, specifically the 'public DataTable PivotTable(DataTable source)' method.

    Phil


    If it works for you, please mark as answered...

    P. Huhn


    Friday, June 10, 2011 12:02 PM
  • Thanks for reply.Actually there is slight change in requirement.Now i am not suppose to use pivot tables.Instead of that only DataTables i am using and in WCf service contract i have to convert that datatable object into generic list.Can we create properties at runtime in Data Contract class.Will it work?
    Sunday, June 12, 2011 4:24 PM
  •  

    Hi,

     

    A general way to achieve your purpose as below:

     

    private List<String> DataTableToList(DataTable dt)
            {
                List<
    string> str = new List<string>();
               
    foreach (DataRow row in dt.Rows)
                {
                   
    foreach (DataColumn col in dt.Columns)
                    {
                        str.Add(row[col].ToString());
                    }

                }

                return str;
            }

    Moreover, we can use LinQ technology to do that, you can post thread in ADO.NET DataSet forum to get tech-support.

     

    Have a nice day!

    Sincerely,


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Paul Zhou Friday, June 17, 2011 5:35 AM
    Monday, June 13, 2011 8:04 AM