【C#】自定义新建一个DataTable(3列),循环3维矩形数组往其填充数据

从中可以了解DataTable的新增行和列;矩形多维数组循环机制;新建了DataTable
 
            DataTable dt = new DataTable();
            DataColumn dc1 = new DataColumn("name", Type.GetType("System.String"));
            DataColumn dc2 = new DataColumn("sex", Type.GetType("System.String"));
            DataColumn dc3 = new DataColumn("age", Type.GetType("System.String"));
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            dt.Columns.Add(dc3);
            object[, ,] arrStr = { { { "Lily", "Females", "20" } }, { { "Jack", "Males", "25" } }, { { "Bob", "Males", "30" } } };
            for (int i = 0; i < arrStr.GetLength(0); i++)
            {
                for (int j = 0; j < arrStr.GetLength(1); j++)
                {
                    DataRow dr = dt.NewRow();
                    for (int k = 0; k < arrStr.GetLength(2); k++)
                    {
                        dr[k] = arrStr[i, j, k];
                    }
                    dt.Rows.Add(dr);
                }
            }

 

posted @ 2015-05-21 14:24  有来技术  阅读(1252)  评论(0编辑  收藏  举报