可以在for循环中添加一个计数器变量来实现自动填充序号。例如:

int count = 1; for (int i=0; i < 16; i++) { string tableName = $"{ProductProductionRecorderTableMainName}{i.ToString()}"; string sql= $@"select * from {tableName} where {ProductionDate} >= '{StartTime}' and {ProductionDate} <= '{EndTime}' ORDER BY [{DaliyOutputTable.Columns[(int)ENUM_ProductionDataRecordTableColumnIndex.DateTime].ColumnName}] DESC"; DataTable dataTable = SqlConnector.GetDataTable(sql); Console.WriteLine($"表{i}中查询到{SqlConnector.GetDataTable(sql).Rows.Count}条记录。"); dataTable.Columns.Add("序号", typeof(int)); // 添加序号列 foreach (DataRow row in dataTable.Rows) { row["序号"] = count++; // 给每行添加序号 } DaliyOutputTable.Merge(dataTable); }

这样就可以在dataTable中添加一个序号列,并且通过计数器变量count来自动填充序号了

自动填充c#中的 for int i=0; i 16; i++ string tableName = $ProductProductionRecorderTableMainNameiToString; string sql= $select from tableName

原文地址: https://www.cveoy.top/t/topic/c1gk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录