• How to convert Generic List to DataTable in C#

    by  • May 20, 2014 • Microsoft .NET C# (C-Sharp), Microsoft ASP.NET • 0 Comments

    DataTable is a famous ADO.NET object where we store data in a table format. It contains data in rows and columns like a table that is why it is considered to be an easy way of getting data from database or other data source and manipulating it in the code to display it on the page. So DataTable object is widely used in ASP.NET code for storing and manipulating data. Sometimes we need to convert our data into DataTable format from another format. Generic List is a collection of objects that contains data for a specific type of object. Few days back, I have written an article about Generic List, ArrayList and difference between these two. You can visit my article if you want to learn more about Generic List.

    I faced a scenario where I need to convert my generic list data into a DataTable so I have written simple code to convert generic list to DataTable. My actual code was bit different but here I am showing you simple code so that you can easily understand it. You just need a method from following to code to convert your generic list to DataTable. So let’s see the code.

    Add following Namespaces

    Write a class to create generic list object

    Write following method where we pass generic list as parameter and it returns a DataTable

    Now create generic list object, fill data and call the method to convert it to DataTable

    Best WordPress Themes and Plugins with Great Team and Support!

    Avatar of GetCodeSnippet.com


    GetCodeSnippet.com provides you high quality Microsoft .NET development related articles, tutorials and sample code for ASP.NET, C#, VB.NET, AJAX, WCF, WPF, Web Services, Windows Services, SQL Server and other technologies.

    Leave a Reply