我需要遍历datatable中的一列,并对其进行一些字符串操作。有没有人能给我举个例子,告诉我如何在表中循环并更新一些数据?
发布于 2014-01-16 18:12:26
如果你想改变指定的列,你可以使用上面的代码,但是如果你想改变datatable中每个单元格的内容,那么我们需要创建另一个Datatable,并按如下所示使用"Import Row“绑定它,如果我们不创建另一个表,它将抛出一个异常,说"Collection was Modified”。
考虑下面的代码。
//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);
}这将使所有单元格都以括号({)开头
https://stackoverflow.com/questions/4108290
复制相似问题