datatable增加自增列

 

        /// <summary>
        /// datatable增加自增列
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DataTable AddAutoIdColumn(DataTable dt)
        {

            if (dt != null)
            {

                DataColumn autoColumn = new DataColumn("AutoID", System.Type.GetType("System.Int32"));

                dt.Columns.Add(autoColumn);

                dt.Columns["AutoID"].SetOrdinal(0);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i][0] = i+1;
                }

            }

            return dt;

        }

 

posted @ 2011-04-18 15:52  dodo-yufan  阅读(4972)  评论(0编辑  收藏  举报