悟生慧

 

动态给DataTable添加行和列

/// <summary>
        /// 给DataTable添加新的行和列
        /// </summary>
        public void DataTableChangeDt()
        {
            //---------********动态给DataTable添加数据*********---------
            newtb = new System.Data.DataTable();
            DataColumn column = new DataColumn("column1", typeof(string));//新增列
            newtb = dt;
            newtb.Columns.Add(column);
           
            for (int i = 0; i < newtb.Rows.Count; i++)
            {
                DataRow newRow = newtb.NewRow();
                newtb.Rows[i]["column1"] = num[i].ToString();
            }
            //--------添加行
           
            DataRow NewRows = newtb.NewRow();
            //NewRows.ItemArray = new object[] { "1", "你好", "45.12" };
            string[] summaryNew = new string[summaryColleciton.Length];
            for (int i = 0; i < summaryColleciton.Length; i++)
            {
                summaryNew[i] = summaryColleciton[i];
            }
            summaryNew[0] = "合计";
            NewRows.ItemArray = summaryNew;
            newtb.Rows.Add(NewRows);
            //---------********动态给DataTable添加数据*********---------
        }

posted on 2011-06-07 10:00  悟生慧  阅读(11179)  评论(1编辑  收藏  举报

导航