using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; namespace dataTable自增列 { class DataTableID { /// <summary> /// 为dt表添加一个自增的ID字段 /// </summary> /// <param name="dt">用户传进来的表</param> /// <returns></returns> public static DataTable AddAutoIdColumn(DataTable dt) { if (dt != null) { //Type.GetType (String) 获取具有指定名称的 Type,运行区分大写和小写的搜索。DataColumn column = new DataColumn("ID", Type.GetType("System.Int32")); //或者这样的形式 //DataColumn column=new DataColumn("AutoID",typeof(int)); dt.Columns.Add(column); dt.Columns["AutoID"].SetOrdinal(0); for (int i = 0; i < dt.Rows.Count; i++) { dt.Rows[i][0] = i + 1; } } return dt; } } class Program { static void Main(string[] args) { } } }