如何计算datagridview列的列中的特定值 [英] How to count specific values in a column of datagridview column
问题描述
在 DataGridView
中,我需要计算一列有多少重复值。
In a DataGridView
I need to count how many duplicate values a column has.
这是我的Datagridview :
This is my Datagridview:
例如,我想计算我的 RisFin列中有多少个 X,并将结果放在文本框中。
For example, I'd like to count how many "X" I have in my "RisFin" column, and put the result in a textbox.
推荐答案
您可以这样计算您需要的内容:
You can count what you need this way:
var count= this.dataGridView1.Rows.Cast<DataGridViewRow>()
.Count(row => row.Cells["RisFin"].Value == "X");
this.textBox1.Text = count.ToString();
使用linq查询,您可以对网格进行很多操作。关键是要使用 Cast<将
,然后您可以使用linq对它执行任何查询。 Rows
集合转换为 IEnumerable< DataGridViewRow>
。 DataGridViewRow>()
Using linq queries you can simply do many things with your grid. and the key point is casting the Rows
collection to an IEnumerable<DataGridViewRow>
using Cast<DataGridViewRow>()
, then you can perform any query on it, using linq.
这篇关于如何计算datagridview列的列中的特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!