C# HashTable To DataTable

  • 863
  • 0
  • 2017-11-28

C# HashTable To DataTable

 

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
	dataTable.Rows.Add(dr);
	//return the DataTable
	return dataTable;
}

資料來源 : 

http://jomemax.blogspot.tw/2013/04/convert-hashtable-rows-into-datatable.html