Click here to Skip to main content
Licence CPOL
First Posted 26 Apr 2004
Views 44,480
Bookmarked 14 times

Convert a Collection to a Table

By | 17 May 2007 | Article
A collection to table in one function.

Introduction

You could use this code to transfer business objects back to tables and to do XML joins or select statements and filters on them. You could also use an XML serializer to do the same thing. Which is better, that depends. I would say this way, because you have the source, and it's really super simple, and quite fast for small tables. The XML serializer can give you some huge headaches if you have written your own collection class that supports IBindingList. (Actually, there are known bugs in it.) If you have a very large table, you would want to use the XML serializer. (At least, I would hope that it would be more optimized than this way.)

That said, you probably should never be in a situation where you need this, but that is a whole other topic.

Let's get to it.

  1. Use Reflection to resolve the properties of an item in a collection.
  2. System.Reflection.PropertyInfo [] propInfo = alist[0].GetType().GetProperties();

    Now you have an array of property names (propInfo).

  3. Add those properties as columns to a table:
  4. dt.Columns.Add(propInfo[i].Name);

What we do now is get the value of the collection item property and set that into the correct row. To do that, invoke the property of the collection's item with InvokeMember.

You will receive an object (object t), and all you have to do is cast that to the correct type and store it in the corresponding row.

Note: This sample just uses ToString, which may not be what you want to do because you have a string as the data type for every column. However, I just use this for creating some reports from business objects, so for that, it works perfectly.

for(int row =0;row < alist.Count ; row++)
{
    dr = dt.NewRow();
    for (int i=0;i< propInfo.Length;i++)
    {
        object tempObject =alist[row];
        object t =tempObject.GetType().InvokeMember(propInfo[i].Name,
                     R.BindingFlags.GetProperty , null,tempObject , new object [] {});

        if (t!=null)
          dr[i] = t.ToString(); 
    }
    dt.Rows.Add(dr);

Complete function

Here is the complete code:

private DataTable CreateDataSource(ArrayList alist) 
{
    DataTable dt = new DataTable();

    if (!alist[0])
        throw new FormatException("Parameter ArrayList empty");

    dt.TableName = alist[0].GetType().Name;
    DataRow dr;
    System.Reflection.PropertyInfo [] propInfo = 
                      alist[0].GetType().GetProperties();

    for(int i=0; i< propInfo.Length;i++)
    {
        dt.Columns.Add(propInfo[i].Name);
    }

    for(int row =0;row < alist.Count ; row++)
    {
        dr = dt.NewRow();

        for (int i=0;i< propInfo.Length;i++)
        {
            object tempObject =alist[row];

            object t =tempObject.GetType().InvokeMember(propInfo[i].Name,
                     R.BindingFlags.GetProperty , null,tempObject , new object [] {});

            if (t!=null)
                dr[i] = t.ToString(); 
        }
        dt.Rows.Add(dr);
    } 
    return dt;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

rj45

Software Developer (Senior)

Canada Canada

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralWorks Grand - once all suggestions are incorporated Pinmemberpippyn4:20 4 Feb '09  
GeneralUse IList as parameter type PinmemberDan Herbert4:26 31 May '08  
GeneralRe: Use IList as parameter type Pinmemberrj458:54 31 May '08  
GeneralLittle improvement PinsussAnonymous0:44 8 Jun '05  
GeneralGetValue PinmemberTutu15:54 28 Apr '04  
GeneralRe: GetValue Pinmemberrj4518:27 28 Apr '04  
GeneralRe: GetValue Pinmemberleeym200422:35 28 Sep '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 17 May 2007
Article Copyright 2004 by rj45
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid