7 Ağustos 2015 Cuma

Object to DataTable in C#



Sample class:

    public class Ogrenci
    {
        public string Ad { get; set; }

        public string IkinciAd { get; set; }

        public string Soyad { get; set; }

        public int Yas { get; set; }

        public string Okul { get; set; }

        public string getirOgrenciIsmi()
        {
            return this.Ad + (String.IsNullOrEmpty(IkinciAd) ? string.Empty : (" " + this.IkinciAd)) + " " + this.Soyad;
        }
    }

Sample objects:

            Ogrenci ogrenci = new Ogrenci();
            ogrenci.Ad = "Ali";
            ogrenci.IkinciAd = "Veli";
            ogrenci.Soyad = "Soyisim";
            ogrenci.Okul = "Yıldız Teknik Üniversitesi";
            ogrenci.Yas = 20;

            Ogrenci ogrenci2 = new Ogrenci();
            ogrenci2.Ad = "Ahmet";
            ogrenci2.IkinciAd = "Mehmet";
            ogrenci2.Soyad = "Soyad";
            ogrenci2.Okul = "İstanbul Teknik Üniversitesi";
            ogrenci2.Yas = 25;

Object to DataTable conversion method:

        public DataTable ToDataTable<T>(T objectT)
        {
            DataTable dataTableResult = new DataTable(typeof(T).Name);
            PropertyInfo[] objectProperites = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo prop in objectProperites)
            {
                dataTableResult.Columns.Add(prop.Name, prop.PropertyType);
            }

            if (objectT != null)
            {
                var values = new object[objectProperites.Length];
                for (int i = 0; i < objectProperites.Length; i++)
                {
                    values[i] = objectProperites[i].GetValue(objectT, null);
                }
                dataTableResult.Rows.Add(values);
            }

            return dataTableResult;
        }

Object to DataTable result:

            DataTable dtResult = ToDataTable<Ogrenci>(ogrenci);



 
 
Object List:

            List<Ogrenci> ogrenciList = new List<Ogrenci>();
            ogrenciList.Add(ogrenci);
            ogrenciList.Add(ogrenci2);



Object list to DataTable conversion method:

        public DataTable ToDataTable<T>(List<T> objectTList)
        {
            DataTable dataTableResult = new DataTable(typeof(T).Name);
            PropertyInfo[] objectProperties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo prop in objectProperties)
            {
                dataTableResult.Columns.Add(prop.Name, prop.PropertyType);
            }

            if (objectTList != null)
            {
                foreach (T item in objectTList)
                {
                    var values = new object[objectProperties.Length];
                    for (int i = 0; i < objectProperties.Length; i++)
                    {
                        values[i] = objectProperties[i].GetValue(item, null);
                    }
                    dataTableResult.Rows.Add(values);
                }
            }

            return dataTableResult;
        }
 



Object List to DataTable result:

            DataTable dtResult2 = ToDataTable<Ogrenci>(ogrenciList);