tags:

views:

219

answers:

3

I'm using ADO.NET and C#, and I want to convert a DataTable object into an array of DataRows. What is an elegant way of doing this?

+3  A: 

My first question would be why? The request makes no sense.

The answer is:

DataRow[] rows = myDataTable.Select();
Joel Etherton
A: 

Actually the DataTable has a property called Rows, witch provides the methods to do this.

You can accomplish this doing:

List<System.Data.DataRow> r = d.Rows.AsQueryable().OfType<System.Data.DataRow>().ToList();
MF
+2  A: 

DataTable.Select() gives you an array of DataRows. You can use this as an array

Dim dt As New DataTable
Dim dr() As DataRow = dt.Select() 

In case you want an ArrayList, you can

public ArrayList ConvertDT(ref DataTable dt)
{
        ArrayList converted = new ArrayList(dt.Rows.Count);
        foreach (DataRow row in dt.Rows)
        {
                converted.Add(row);
        }
        return converted;
}

I have not used the dt.rows.CopyTo function. maybe that works also.