编辑DataTable单元格
本文关键字:单元格 DataTable 编辑 | 更新日期: 2025-01-25 13:00:00
我想编辑DataTable中列中的单元格我有
DataTable theDataTable = new DataTable();
theDataTable.Columns.Add("Column1", typeof(string));
theDataTable.Columns.Add("Column2", typeof(string));
theDataTable.Columns.Add("Column3", typeof(string));
它从一个文本文件中获取数据,所以它看起来像这个
Column1 Column2 Column3
2015-03-23 T_Someinfo 040-555555
2015-03-24 T_Someinfo 040-666666
2015-03-23 T_Someinfo 040-666666
现在我想在第3列中搜索"-"并将其删除。所以第3列中的结果是这样的。
Column3
040555555
040666666
040666666
如何搜索"-"并将其从DataTable的单元格中删除?
迭代抛出Rows
并修改每个单元格,如:
foreach (DataRow row in theDataTable.Rows)
{
if (row["Column3"] != null)
row["Column3"] = row["Column3"].ToString().Replace("-", "");
}
你可以试试这样的东西:
// We iterate through the DataTable rows.
foreach(DataRow row in theDataTable .Rows)
{
// We get the value of Column3 for the current row and replace
// the - with empty.
string value = row.Field<string>("Column3").Replace("-","");
// Then we update the value.
row.SetField("Column3", value);
}