编辑 DataTable 单元格
Edit DataTable cell
我想编辑我的 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
现在我想在 Column3 中搜索“-”并将其删除。
所以 Column3 中的结果将如下所示。
Column3
040555555
040666666
040666666
如何搜索“-”并将其从 DataTable 的单元格中删除?
您可以尝试这样的操作:
// 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);
}
迭代抛出 Rows
并像这样修改每个单元格:
foreach (DataRow row in theDataTable.Rows)
{
if (row["Column3"] != null)
row["Column3"] = row["Column3"].ToString().Replace("-", "");
}
我想编辑我的 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
现在我想在 Column3 中搜索“-”并将其删除。 所以 Column3 中的结果将如下所示。
Column3
040555555
040666666
040666666
如何搜索“-”并将其从 DataTable 的单元格中删除?
您可以尝试这样的操作:
// 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);
}
迭代抛出 Rows
并像这样修改每个单元格:
foreach (DataRow row in theDataTable.Rows)
{
if (row["Column3"] != null)
row["Column3"] = row["Column3"].ToString().Replace("-", "");
}