在C#中遍历并更新DataTable可以通过使用foreach循环和DataRow对象来实现。以下是一个示例代码:
// 创建一个DataTableDataTable dt = new DataTable();dt.Columns.Add("ID", typeof(int));dt.Columns.Add("Name", typeof(string));// 添加一些数据到DataTabledt.Rows.Add(1, "Alice");dt.Rows.Add(2, "Bob");// 遍历并更新DataTableforeach (DataRow row in dt.Rows){ // 更新Name列的值 row["Name"] = "Updated " + row["Name"];}// 打印更新后的DataTableforeach (DataRow row in dt.Rows){ Console.WriteLine(row["ID"] + " " + row["Name"]);}在上面的示例中,我们首先创建了一个包含两列的DataTable,并向其添加了两行数据。然后使用foreach循环遍历DataTable中的每一行,通过DataRow对象来更新Name列的值。最后再次遍历DataTable以打印更新后的数据。


