ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

ToHashTable

Converts a DataRow into a Hashtable.

Source

/// <summary>
/// Converts a DataRow object into a Hashtable object, 
/// where the key is the ColumnName and the value is the row value.
/// </summary>
/// <param name="dr"></param>
/// <returns></returns>
public static Hashtable ToHashTable(this DataRow dr)
{
    Hashtable htReturn = new Hashtable(dr.ItemArray.Length);
    foreach (DataColumn dc in dr.Table.Columns)
        htReturn.Add(dc.ColumnName, dr[dc.ColumnName]);

    return htReturn;
}

Example

Hashtable ht = myDataRow.ToHashTable();

Author: Jonnidip

Submitted on: 8 jun. 2009

Language: C#

Type: System.Data.DataRow

Views: 6653