Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to convert List to datatable

How to convert List to datatable

Scheduled Pinned Locked Moved C#
helptutorialquestion
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NK7
    wrote on last edited by
    #1

    Hi I have one Generic List how to convert the list into data table?. Any one help me please?. Regards Kesavan

    kesavan

    A I H 3 Replies Last reply
    0
    • N NK7

      Hi I have one Generic List how to convert the list into data table?. Any one help me please?. Regards Kesavan

      kesavan

      A Offline
      A Offline
      Ashfield
      wrote on last edited by
      #2

      AFAIK there is only the long way - loop through the list and add each item

      Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

      P 1 Reply Last reply
      0
      • A Ashfield

        AFAIK there is only the long way - loop through the list and add each item

        Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

        P Offline
        P Offline
        Paulo Zemek
        wrote on last edited by
        #3

        I don't really know why you need it. In many cases is better to loop throght the items and execute the query directly. But: You can generate a common code that: Uses Reflection to discover the properties/fields of the classes. Be able to map the fields to the columns with the same name or, to create such columns in the datatable (that depends on your need). Then, loop at each item adding a row and loop for each field/property setting the right column value.

        1 Reply Last reply
        0
        • N NK7

          Hi I have one Generic List how to convert the list into data table?. Any one help me please?. Regards Kesavan

          kesavan

          I Offline
          I Offline
          i j russell
          wrote on last edited by
          #4

          CSLA has/had a class called ObjectAdaptor that was designed to do what you want.

          1 Reply Last reply
          0
          • N NK7

            Hi I have one Generic List how to convert the list into data table?. Any one help me please?. Regards Kesavan

            kesavan

            H Offline
            H Offline
            hamed vojdani
            wrote on last edited by
            #5

            What type of list? For list of int you can do it like this:

                    List<int> myList = new List<int>(100);
                    for (int i = 1; 1 <= 50; i++)
                    {
                        myList.Add(i);
                    }
            
                    // Create DataTable without column and row
                    DataTable dt = new DataTable("MyTbl"); 
            
                    // Add an integer Column
                    dt.Columns.Add("MyColumn", Type.GetType("System.Int32")); 
                    
                    for (int j = 0; j < myList.Count; j++) // fill DataTable 
                    {
                        dt.Rows.Add(myList\[j\]);
                    }
            
            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups