.net 如何在 BindingSource.Filter 之后获取 DataGridView 的可见行数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/509578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 11:10:26  来源:igfitidea点击:

How to get visible row count of DataGridView after BindingSource.Filter?

.netxmlwinformsdatagridviewc#-2.0

提问by Jared Updike

I have a table with say 1640 items. I set

我有一张桌子,上面有 1640 件物品。我设置

bindingSource.Filter = "some filter query string";

and most of the rows disappear, leaving, say, 400 rows. I'd like to be able to tell the user "Showing 400 of 1640 items" as they click some textboxes which change the filter string and hence which rows are visible in the dataGridView object (much like iTunes but for medical data, not genres/artists/albums filtering songs).

并且大部分行都消失了,只剩下 400 行。我希望能够告诉用户“显示 1640 个项目中的 400 个”,因为他们单击了一些文本框,这些文本框更改了过滤器字符串,因此在 dataGridView 对象中可以看到哪些行(很像 iTunes,但用于医疗数据,而不是流派/艺术家/专辑过滤歌曲)。

I tried bindingSource.Count and it is always 1640 no matter what the Filter string is set to (even though many fewer rows are shown as desired). I tried looping over all the rows in dataGridView.Rows and counting only the rows that are Visible, but that still sums to 1640.

我尝试了 bindingSource.Count 并且无论过滤器字符串设置为什么,它始终为 1640(即使根据需要显示的行数更少)。我尝试遍历 dataGridView.Rows 中的所有行并仅计算可见的行,但总和仍为 1640。

Where can I get this information?

我可以从哪里获得这些信息?

Note that I am not using SQL but bindingSource.DataSource is a DataSource from a DataView wrapped around a DataTable (from a dataSet read from XML).

请注意,我没有使用 SQL,但 bindingSource.DataSource 是来自 DataView 包裹在 DataTable 中的 DataSource(来自从 XML 读取的数据集)。

采纳答案by Picflight

How about adding the filtered items in a separate DataTable and doing a count on that for the filtered items.

如何在单独的 DataTable 中添加过滤的项目并对过滤的项目进行计数。

回答by ThunderGr

Try this: datagridviewname.Rows.GetRowCount(DataGridViewElementStates.Visible);

尝试这个: datagridviewname.Rows.GetRowCount(DataGridViewElementStates.Visible);

回答by Jay Riggs

Jared,

贾里德,

I recently had to do this very thing. What worked for me was using the DataGridView.Rows.Count property after I applied the filter.

我最近不得不做这件事。对我有用的是在应用过滤器后使用 DataGridView.Rows.Count 属性。

Are you setting your data source to the DataSource property of the BindingSource or the DataGridView? It should be the BindingSource.

您是否将数据源设置为 BindingSource 或 DataGridView 的 DataSource 属性?它应该是 BindingSource。

HTH -Jay

HTH-杰伊

回答by Jared Updike

I screwed something else up. Jay is right:

我搞砸了别的东西。杰是对的:

dataGridViewCases.Rows.Count

works, as does:

有效,就像:

bindingSource.Count

回答by S.Mohamed Mahdi Ahmadian zadeh

you need just use Count property of your bindingSource...

您只需要使用 bindingSource 的 Count 属性...

ExampleBindoneSource.Count()