简体   繁体   English

数据集行号(仅带填充行)

[英]Dataset row number w/ filled rows only

如果我使用ds.Tables[0].Rows.Count ,则无论行中是否有数据,我都会得到行数,我不想对空,空白或null的行进行计数,是否有任何办法它实际上没有逐行阅读吗?

yes- DataTable implements the IEnumerable interface, which means that you can use LINQ to query it: 是- DataTable实现IEnumerable接口,这意味着你可以使用LINQ查询它:
ds.Tables[0].Count(row => !string.IsNullOrEmpty(row["Name"]) /*etc..*/);
(if you don't already know linq- you should familiarize yourself . it's great.) (如果您还不知道linq,则应该熟悉一下自己 。这很棒。)

if you manage to isolate the rows you want with a filter you can use any of these approaches: 如果您设法使用过滤器隔离所需的行,则可以使用以下任何一种方法:

DataView myView = new DataVie(ds.Tables[0]);

myView.RowFilter = "NAME IS NOT NULL";

int count = myView.Count;

or also with Select... 或选择...

DataRow[] myRows = ds.Tables[0].Select("NAME IS NOT NULL");

int count = myRows.Length;

then in the first case you can iterate on the DataView and in the second case you iterate on the DataRow array. 那么在第一种情况下,您可以在DataView上进行迭代,而在第二种情况下,您可以在DataRow数组上进行迭代。 For binding to UI Controls DataView is probably better. 对于绑定到UI控件,DataView可能更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM