我需要循环遍历我的数据表格中的一列并对其进行一些字符串操作。请问是否可以给我一个例子,演示如何循环遍历表格并更新一些数据?
foreach (DataRow row in MyDataTable.Rows)
{
row["columnNameHere" Or index] = value;
}
foreach (DataRow row in myDataTable.Rows)
{
//do what you need to calculate myNewValue here
row["myColumn"] = myNewValue;
}
已更新以添加 .Rows.
使用for循环完成相同的事情。这只是个人偏好问题。尝试以下代码...
for (int i = 0; i <= myDataTable.Rows.Count - 1; i++){
myDataTable.Rows[i]["ColumnName" Or IndexNumber] = value;
}
//New Datatable created which will have updated cells
DataTable dtUpdated=new DataTable();
//This gives similar schema to the new datatable
dtUpdated = dtReports.Clone();
foreach (DataRow row in dtReports.Rows)
{
for (int i = 0; i < dtReports.Columns.Count; i++)
{
string oldVal = row[i].ToString();
string newVal = "{"+oldVal;
row[i] = newVal;
}
dtUpdated.ImportRow(row);
}
这将使所有以括号({)开头的单元格生效。