Ilkin Turk

Ilkin Turk

  • NA
  • 349
  • 160.8k

IList convert to datatable

Jul 3 2013 10:39 AM
Hello.

I need  to convert Ilist to datatable using below method.But couldnt.Please help .


Here when I select items from checkbox of ultracomboeditor - I need to add them to datatable.So

 this.ultraComboEditor1.CheckedListSettings.CheckBoxStyle = CheckStyle.CheckBox;





 System.Collections.IList values = null;
   
                values = this.ultraComboEditor1.Value as System.Collections.IList;


my Problem is to convert the ILIst to datatable.

using below method I want to convert IList to datatable.Please show how.

  public DataTable ToDataTables<T>(IList<T> data)
        {
            PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            for (int i = 0; i < props.Count;i++ )
            {
                PropertyDescriptor prp = props[i];
                table.Columns.Add(prp.Name,prp.PropertyType);
            }
            object[] values = new object[props.Count];
            foreach(T item in data)
            {
                for (int i = 0; i < values.Length;i++ )
                {
                    values[i] = props[i].GetValue(item);
                }
                table.Rows.Add(values);
            }
            return table;
        }


Thanks in advance.
                




Answers (3)