CS.NET - Convert an ArrayList to a DataTable



Cheat sheet to Convert an ArrayList to a DataTable

private void ArrayListtoDataTable()
{
            //Create ArrayList.
            ArrayList alSyntaxHelp = new ArrayList();
            alSyntaxHelp.Add("The");
            alSyntaxHelp.Add("quick");
            alSyntaxHelp.Add("brown");
            alSyntaxHelp.Add("fox");

            // Create new DataTable.
            DataTable table = new DataTable("table");

            // Declare DataColumn and DataRow variables.
            DataColumn column;
            DataRow row;

            // Create column.
            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "SyntaxHelp";
            table.Columns.Add(column);

            // Add items from ArrayList to DataTable.   
            foreach (String item in alSyntaxHelp)
            {
                row = table.NewRow();
                row["SyntaxHelp"] = item;
                table.Rows.Add(row);
            }
}

Share |

 Cant find the page you are looking for?
 Help us to improve by adding the content that you are looking for.
 Leave a feedback
 We look forward to hear your comments and feedback.