首页 > 代码库 > 清除DataTable中的空行记录

清除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]);}

 

清除DataTable中的空行记录