Horje
fill dataset with list c# Code Example
fill dataset with list c#
public class ListtoDataTableConverter
{
    public DataTable ToDataTable<T>(List<T> items)
    {
        DataTable dataTable = new DataTable(typeof(T).Name);
        //Get all the properties
        PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
        foreach (PropertyInfo prop in Props)
        {
            //Setting column names as Property names
            dataTable.Columns.Add(prop.Name);
        }
        foreach (T item in items)
        {
            var values = new object[Props.Length];
            for (int i = 0; i < Props.Length; i++)
            {
                //inserting property values to datatable rows
                values[i] = Props[i].GetValue(item, null);
            }
            dataTable.Rows.Add(values);
        }
        //put a breakpoint here and check datatable
        return dataTable;
    }
}




Csharp

Related
excute same code mvc Code Example excute same code mvc Code Example
how to authorize token when consuming api in c# Code Example how to authorize token when consuming api in c# Code Example
c# azure get vm get cpu usage Code Example c# azure get vm get cpu usage Code Example
c# e-mail send Code Example c# e-mail send Code Example
Hovercraft Physics in Unity Code Example Hovercraft Physics in Unity Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
13