spongebob_c# 0 Newbie Poster

Hello all,

im new here and i need help. Here is my code first:

DataTable dataTable = new DataTable();
            DataColumnCollection drc = dbDataSet.Tables["TableName"].Columns;
            int i = 0;

            dataTable.Columns.Add(" ", typeof(string));

            foreach (DataColumn dc in drc)
            {
                if (dc.ColumnName != "ID")
                {
                    dataTable.Columns.Add(dc.ColumnName, typeof(string));
                    i++;
                }
            }

            DataRowCollection dra = dbDataSet.Tables["TableName"].Rows;
            
            foreach (DataRow dr in dra)
            {
                ArrayList atmp = new ArrayList();
                for (int j = 0; j < i; j++)
                {
                    if(dr[j+1] != DBNull.Value)
                    {
                        atmp.Add(dr[j+1]);
                    }
                    else
                    {
                        atmp.Add("");
                    }
                }

                dataTable.Rows.Add(new object[] { atmp[0], atmp[1], atmp[2], atmp[3] });
            }

            dgw.DataSource = dataTable;
            dgw.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

            con.Close();

I have an ArrayList containing data from Database that are not DBNULL and i want to pass it as a new object into DataGridView. Since Columns Count can be variable, how can i dinamically expand this new object[] so it can be variable depending on Column Count:


I could accomplish it in static bad way, using new object with length of column count, for ex.:

switch (i)
{
case 1:
    dataTable.Rows.Add(new object[] { atmp[0] });
    break;
case 2:
    dataTable.Rows.Add(new object[] { atmp[0], atmp[1]});
    break;
case 3:
    dataTable.Rows.Add(new object[] { atmp[0], atmp[1], atmp[2] });
    break;
case 4:
    dataTable.Rows.Add(new object[] { atmp[0], atmp[1], atmp[2], atmp[3] });
    break;
}
.
.
.
.
.

Is there a dynamic way of this issue?