Create DataColumn - CSharp System.Data

CSharp examples for System.Data:DataColumn

Description

Create DataColumn

Demo Code


using System.Reflection;
using System.Data;
using System.ComponentModel;
using System.Collections.Generic;
using System;/*ww w.jav a 2 s.c o m*/

public class Main{
        public static T CreateItem<T>(this DataRow row)
        {
            if (row == null) return default(T);

            T obj = Activator.CreateInstance<T>();

            foreach (DataColumn column in row.Table.Columns)
            {
                PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
                object value = row[column.ColumnName];
                prop.SetValue(obj, value, null);
            }

            return obj;
        }
}

Related Tutorials