删除DataTable Empty Rows [英] Delete DataTable Empty Rows
本文介绍了删除DataTable Empty Rows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我对处理DataTable有疑问。我有一个DataTable,在我的数据表中我有100行。但是在100之外,只有5行数据剩余所有行都是空的。
现在我的问题是我要删除所有剩余的95行。哪一行没有数据。
怎么办可以帮助我解决这个问题...
Hi everybody,
I have a doubt in handling the DataTable. I have a DataTable, in my datatable i have 100 rows. But out 100 , only 5 rows having data remaining all rows are empty.
Now my question is i want to remove all remaining 95 rows. which row doesn't have a data.
How to do can anybody help me regarding this issue...
推荐答案
在流动的示例中,您的实际数据存储在dt中。
Hi, in flowing example your actual data is stored in dt.
DataTable dt1=dt.Clone();
var rows =from row in dt.AsEnumerable()
where row.Field<string>("columnname") != null
select row;
foreach (DataRow dr in rows)
{
dt1.Rows.Add(dr);
}
</string>
你可以创建dt到dt1的副本并使用链接你可以找到没有null的行value
you can create copy of dt to dt1 and using link you can find row which dose not have null value
这是来自fjdiewornncalwe的答案的更正版本。我删除了逻辑&我在使用该代码时遇到的语法错误。
It is corrected version of answer from fjdiewornncalwe. I removed logical & syntax errors i faced while using that code.
private void RemoveEmptyRows(DataTable source)
{
for (int i = source.Rows.Count; i >= 1; i--)
{
DataRow currentRow = source.Rows[i - 1];
foreach (var colValue in currentRow.ItemArray)
{
if (!string.IsNullOrEmpty(colValue.ToString()))
break;
// If we get here, all the columns are empty
source.Rows[i - 1].Delete();
}
}
}
如果你需要在没有linq的情况下这样做,你可以试试这个。
注意:我刚刚在这里敲了一下,我还没有彻底检查语法。
If you need to do it without linq, you can try this.
NOTE: I did just hammer this out here, I haven't thoroughly checked the syntax.
private void RemoveEmptyRows(DataTable source)
for( int i = source.Rows.Count; i >= 0; i-- )
{
DataRow currentRow = source.Rows[i];
foreach( var colValue in currentRow.ItemArray)
{
if( !string.IsNullOrEmpty(colValue) )
break;
// If we get here, all the columns are empty
source.Rows[i].Delete();
}
}
}
这篇关于删除DataTable Empty Rows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文