List<DataRow> removelist = new List<DataRow>();
for (int i = 0; i < dt.Rows.Count; i++)
{
bool rowdataisnull=true;
for (int j = 0; j < dt.Columns.Count; j++) {
if (dt.Rows[i][j].ToString().Trim() != "") {
rowdataisnull = false;
}
}
if (rowdataisnull) {
removelist.Add(dt.Rows[i]);
}
}
for (int i = 0; i < removelist.Count; i++) {
dt.Rows.Remove(removelist[i]);
}
for (int i = 0; i < dt.Rows.Count; i++)
{
bool rowdataisnull=true;
for (int j = 0; j < dt.Columns.Count; j++) {
if (dt.Rows[i][j].ToString().Trim() != "") {
rowdataisnull = false;
}
}
if (rowdataisnull) {
removelist.Add(dt.Rows[i]);
}
}
for (int i = 0; i < removelist.Count; i++) {
dt.Rows.Remove(removelist[i]);
}
本文介绍了一种方法,通过遍历数据集并检查每行数据是否包含至少一个非空值来实现数据清理。具体步骤包括定义一个布尔变量用于标记行是否为空,然后遍历数据集中每一列,若发现非空值,则置布尔变量为false,完成遍历后,如果该变量仍为true,则将该行加入到要删除的列表中。最后,循环遍历删除标记的行,以优化数据集。这种方法对于处理大型数据集时的数据预处理非常有效。
392

被折叠的 条评论
为什么被折叠?



