http://amitpatriwala.wordpress.com/2008/12/03/convert-generic-list-in-to-datatable/
http://lekhok.com/pages/topic_view.aspx?id=39
Both are Good Stuffs
public static DataTable convert2Table
{
    DataTable table = new DataTable();
    if (list.Count > 0)
    {
        PropertyInfo[] properties = list[0].GetType().GetProperties();
        List<string> columns = new List<string>();
        foreach (PropertyInfo pi in properties)
        {
            table.Columns.Add(pi.Name);
            columns.Add(pi.Name);
        }
        foreach (T item in list)
        {
            object[] cells = getValues(columns, item);
            table.Rows.Add(cells);
        }
    }
    return table;
}
private static object[] getValues(List<string> columns, object instance)
{
    object[] ret = new object[columns.Count];
for (int n = 0; n <>
    {
        PropertyInfo pi = instance.GetType().GetProperty(columns[n]);
        object value = pi.GetValue(instance, null);
        ret[n] = value;
    }
    return ret;
}

3 comments:
請繼續發表好文!加油加油加油! .................................................................
If you can not be kind, at least have the decency to be vague...................................................
Wow these are tips very helpful for me and all of us. Please keep on blogging.online book Ticket through Redbus
Post a Comment