We help IT Professionals succeed at work.
Get Started

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

Olukayode Oluwole
Last Modified: 2020-03-04
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 =
            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;
            return table;

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)

List to DataTable Script
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


Watch Question
Distinguished Expert 2019
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE