Navi's

Necessity, the mother of invention

  • Visitor Map

    Locations of Site Visitors
  • Flag Counter

    free counters

Datatable to Generic collection and vice versa

Posted by Navi's on January 13, 2011


Most of the time we need to convert Generic Collection(GC) to DataTable(DT) and Data Table(DT) to Generic Collection(GC). For that bellow two functions are very helpfull to Convert Generic Function(GC) to DataTable(DT) and vice versa.

To convert DataTable(DT) to Generic Collection(GC) you can use bellow code snippet

Pass the DataTable(DT) object to bellow function and it will return you the Generic Collection(GC) of given type.

 public static List<T> ConvertTo<T>(DataTable table)
        {
            List<T> list = new List<T>();
            if (table == null)
            {
                return null;
            }

            List<DataRow> rows = new List<DataRow>();

            foreach (DataRow row in table.Rows)
            {
                T item = CreateItem<T>(row);
                list.Add(item);
            }

            return list;
        }
 public static T CreateItem<T>(DataRow row)
        {
            Type objType = typeof(T);
            T obj = Activator.CreateInstance<T>();
            if (row != null)
            {
                foreach (DataColumn column in row.Table.Columns)
                {
                    PropertyInfo prop = objType.GetProperty(column.ColumnName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    object value = row[column.ColumnName];
                    if (value.Equals(DBNull.Value))
                        value = null;

                      prop.SetValue(obj, Convert.ChangeType(value, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType), null);
                   
                }
            }

            return obj;
        }

To convert Generic Collection(GC) to DataTable(DT) you can use bellow code snippet.

Pass the List object in bellow function and it will return DataTable(DT).

        public static DataTable ListToDataTable<T>(List<T> list)
        {
            DataTable dt = new DataTable();

            foreach (PropertyInfo info in typeof(T).GetProperties())
            {
                dt.Columns.Add(new DataColumn(info.Name, Nullable.GetUnderlyingType(info.PropertyType) ?? info.PropertyType));
            }
            foreach (T t in list)
            {
                DataRow row = dt.NewRow();
                foreach (PropertyInfo info in typeof(T).GetProperties())
                {
                    row[info.Name] = info.GetValue(t, null) != null ? info.GetValue(t, null) : DBNull.Value;
                }
                dt.Rows.Add(row);
            }
            return dt;
        }

References:
http://bit.ly/gck7q4
http://bit.ly/ebDP0Q

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: