1. 程式人生 > >DataTable中新增一列序號

DataTable中新增一列序號

        /// <summary>
        /// 新增一列序號
        /// </summary>
        /// <param name="dt">需要新增序號列的表</param>
        /// <returns>DataTabel</returns>
        private DataTable AddCol( DataTable dt)
        {
                DataTable dtR;   //返回值
                DataColumn dtNewCol = new DataColumn("Id", typeof(string)); //建立ID列
                dt.Columns.Add(dtNewCol);
                dt.Columns["Id"].SetOrdinal(0);
                dtR= dt;
                int dtc = dtR.Rows.Count;    //dtc  行數
                for (int i = 0; i < dtc; i++)
                {
                    dtE.Rows[i]["Id"] = (i + 1).ToString();
                }
            return dtR;
        }