要查找DataGridView中值在指定范围之间的行数?

时间:2014-06-18 05:38:40

标签: c# datagridview datagridviewcolumn

我的DataGridView的一列包含许多不同的值。

例如:

 columnName
    10
    21
    23
    25
    12
    14
    16
    28
    30
    29
    36
    47
    56
    65
    78
    89
    96
    121
    126
    21
    132
    55
    16
    ... etc

在这里,我需要将0-30,30-60,60-90范围之间的值分开。

    {
    var sector1 = "no.of values lies between 0-30";
    var sector2 = "no.of values lies between 30-60";
    var sector3 = "no.of values lies between 60-90";
    var sector4 = "no.of values lies between 90-120";
     }

我试过了,

     try
     {
     var values = dataGridView1.Rows.Cast<DataGridViewRow>().Select(x => (int)x.Cells[columnName].Value);
     var sector1 = values.Count(x => x >= 0 && x < 30);
     var sector2 = values.Count(x => x >= 30 && x < 60);
     var sector3 = values.Count(x => x >= 60 && x < 90);
     var sector4 = values.Count(x => x >= 90 && x < 120);
     var sector5 = values.Count(x => x >= 120 && x < 150);
     var sector6 = values.Count(x => x >= 150 && x < 180);
     [...]
     MessageBox.Show(sector1.ToString());
     [...]
     }
     catch
     {
      MessageBox.Show("ERROR");
     }

但没有用,我只收到错误通知。

任何人都可以建议语法来查找两个范围之间的行数吗?

1 个答案:

答案 0 :(得分:2)

请试试这段代码

var values = gvtest.Rows.Cast<GridViewRow>().Select(x => x.Cells[0].Text);
var sector1 = values.Count(x => Convert.ToInt32(x) >= 0 && Convert.ToInt32(x) < 30);