Click here to Skip to main content
13,196,126 members (47,595 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 7 Feb 2013

Convert Hashtable Rows into DataTable Columns in C#

, 7 Feb 2013
Rate this:
Please Sign up or sign in to vote.
Function to Convert Hashtable Rows into DataTable Columns in C#.Net


Function to Convert Hashtable Rows into DataTable Columns in C#


Hashtable, DataSet, DataRow,DictionaryEntry etc.

Using the Code 

Simplest way to convert a Hashtable into a DataTable with all the Hashtable rows converted into DataTable columns. This code creates one row in DataTable, you can modify it according to your requirement.

This Function returns a DataTable object and takes a Hashtable object as parameter. Simple implementation example: 

htData <Hashtable with Data>
	DataTable dt =new  DataTable();
	dt=  ConvertHashtableRowsToDataTableColumns(htData);

 The classes used are:  

DataTableDictionaryEntryDataRowobject, var and Hashtable.
the methods and properties, used are: 

  1. dataTable.Columns.Add(entry.Key.ToString(), typeof(object));
  2. entry.Value.ToString()
  3. dataTable.Rows.Add(dr)
  4. dataTable.NewRow()
  5. ht.GetType().Name

and dataTable.TableName, used to specify the table name. 

private DataTable ConvertHashtableRowsToDataTableColumns(System.Collections.Hashtable ht)
            //create an instance of DataTable
            var dataTable = new DataTable(ht.GetType().Name);
            //specify the table name		
            dataTable.TableName = "TableName";
            //fill the columns in the DataTable
            foreach (DictionaryEntry entry in ht)
                dataTable.Columns.Add(entry.Key.ToString(), typeof(object));
            //create a new DataRow in the DataTable	
            DataRow dr = dataTable.NewRow();
            //fill the new row in the DataTable
            foreach (DictionaryEntry entry in ht)
                dr[entry.Key.ToString()] = entry.Value.ToString();
            //add the filled up row to the DataTable
            //return the DataTable
            return dataTable;

Hope this helps in time of need...  

Happy Coding Smile | <img src= 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jome Mathew
Software Developer
United States United States
Software Programmer

You may also be interested in...

Comments and Discussions

QuestionConvert Hashtable into DataTable Rows in C# Pin
Jome Mathew29-Feb-16 11:57
groupJome Mathew29-Feb-16 11:57 
SuggestionI did it a little different Pin
Airton J.21-Aug-13 6:56
memberAirton J.21-Aug-13 6:56 
GeneralRe: I did it a little different Pin
Jome Mathew29-Feb-16 12:03
groupJome Mathew29-Feb-16 12:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171019.1 | Last Updated 7 Feb 2013
Article Copyright 2013 by Jome Mathew
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid