清除DataTable中的空行记录

第一种方法:

string filter = "";
for (int i = 0; i < dt.Columns.Count; i++)
{
    if (i < dt.Columns.Count - 1)
        filter += dt.Columns[i].ColumnName + " IS NULL AND ";
    else
        filter += dt.Columns[i].ColumnName + " IS NULL";
}
var rows = dt.Select(filter);
for (int i = 0; i < rows.Length; i++)
{
    dt.Rows.Remove(rows[i]);
}

第二种方法:

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 (!string.IsNullOrEmpty(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]);
}
原文地址:https://www.cnblogs.com/tomz/p/4062092.html