Link to home
Start Free TrialLog in
Avatar of Olukayode Oluwole
Olukayode OluwoleFlag for Canada

asked on

How can i pass my list to a DataTable and Load same to a Grid

I have a List in my c#  application prepared using LINQ

The list is generated and i need to pass it to a ListToDataTable  processor in my Library

before loading to a Grid

The  List to Datatable Script  in my Library is as shown below

[] public static class ListToDataTableProcessor
    {

        public static DataTable ToDataTable<T>(this IList<T> data)
        {
            PropertyDescriptorCollection properties =
                TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            foreach (PropertyDescriptor prop in properties)
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            foreach (T item in data)
            {
                DataRow row = table.NewRow();
                foreach (PropertyDescriptor prop in properties)
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                table.Rows.Add(row);
            }
            return table;
        }
    }[/code]

My application LINQ  script that generates the List is shown below with the last 2 lines flagged.

I am trying to pass the list to the Processor and return a DataTable  (see screen below)

User generated image
The list generation is ok. I just do not know how to correct the error reported

and how to pass the list to the processor and get the returned Table

I will be Grateful for any Help

Thank

Olukay
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Olukayode Oluwole

ASKER

Thanks .

You always made my day with your proof of concept