

在读取datatable绑定控件时会读到空的行,怎样删除没有数据的行呢,如下
写一个方法遍历table删除空白行
protected DataTable removeEmpty(DataTable dt)
{
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]);
}
return dt;
}
调用removeEmpty(DataTable dt)


本文介绍了一种在C#中从DataTable中删除所有空白行的方法。通过遍历每一行检查其所有列是否为空,将需要删除的行添加到一个列表中,最后统一删除这些行,避免了直接删除过程中引发的索引错误。


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



