c#datagridview';如果';基于上一列的计数条件不起作用

c#datagridview';如果';基于上一列的计数条件不起作用,c#,datagridview,C#,Datagridview,我仍然是一名c#初学者,我试图计算有多少“1”处于状态id,然后根据状态id用1或4或5来计算部门id 所以基本上“status\u id”如果“1”表示它的开放性,我想知道它在哪个部门开放。 但如果条件不起作用 private void button2_Click(object sender, EventArgs e) { var count = this.dataGridView1.Rows.Cast<DataGridViewRow>() .Count(ro

我仍然是一名c#初学者,我试图计算有多少“1”处于
状态id
,然后根据状态id用1或4或5来计算
部门id
所以基本上“status\u id”如果“1”表示它的开放性,我想知道它在哪个部门开放。 但如果条件不起作用

private void button2_Click(object sender, EventArgs e)
{
    var count = this.dataGridView1.Rows.Cast<DataGridViewRow>()
       .Count(row => row.Cells["status_id"].Value.ToString() == "1");

    if (dataGridView1.Rows[count - 1].Cells["status_id"].Value.ToString() == "1")
    {
        var general = this.dataGridView1.Rows.Cast<DataGridViewRow>()
       .Count(row => row.Cells["dept_id"].Value.ToString() == "1");
        this.textBox2.Text = general.ToString();
    }
private void按钮2\u单击(对象发送者,事件参数e)
{
var count=this.dataGridView1.Rows.Cast()
.Count(row=>row.Cells[“status_id”].Value.ToString()=“1”);
if(dataGridView1.Rows[count-1].Cells[“status_id”].Value.ToString()=“1”)
{
var general=this.dataGridView1.Rows.Cast()
.Count(row=>row.Cells[“dept_id”].Value.ToString()=“1”);
this.textBox2.Text=general.ToString();
}
这是第一部分“通用”的代码示例,其余部分与“财务”和“技术”的代码示例相同


您的逻辑在me中似乎很奇怪:在第一行,您计算状态为1的行。例如,您发现3行状态为1的行

然后在找到的计数的位置获得该行(因此在我的示例中,将获得第三行),如果该行的状态等于1,则执行if块中的代码(对dept_id等于1的所有行进行计数)


我真的不明白你想做什么,但我猜你不想提到第n行,其中n是基于状态计数的?

你的逻辑对我来说似乎很奇怪:在第一行,你计算状态等于1的行。例如,你发现3行具有该状态

然后在找到的计数的位置获得该行(因此在我的示例中,将获得第三行),如果该行的状态等于1,则执行if块中的代码(对dept_id等于1的所有行进行计数)

我真的不明白您想做什么,但我猜您不想引用第n行,其中n基于状态计数?

让我们试试这个

private void button2_Click(object sender, EventArgs e)
{
    //get all rows in your dataGrid that have a 1 in status_id
    var foundRows = this.dataGridView1.Rows.Cast<DataGridViewRow>().Where(row => row.Cells["status_id"].Value.ToString() == "1");

    //get the count of the found rows
    var count = foundRows.Count(); 

    //in the foundRows search further/deeper for rows that have a 1 in dept_id - this will give rows that have both status_id = 1 AND dept_id = 1
    var foundChildRows = foundRows.Where(row => row.Cells["dept_id"].Value.ToString() == "1");

    //get the count of the found"Child"Rows
    var childCount = foundChildRows.Count();
    this.textBox2.Text = childCount.ToString();
}
private void按钮2\u单击(对象发送者,事件参数e)
{
//获取dataGrid中状态为1的所有行
var foundRows=this.dataGridView1.Rows.Cast().Where(row=>row.Cells[“status\u id”].Value.ToString()=“1”);
//获取找到的行的计数
var count=foundRows.count();
//在foundRows中,进一步/深入搜索部门id为1的行-这将给出状态为1且部门id为1的行
var foundChildRows=foundRows.Where(row=>row.Cells[“dept_id”].Value.ToString()==“1”);
//获取找到的“子”行的计数
var childCount=foundChildRows.Count();
this.textBox2.Text=childCount.ToString();
}
让我们试试这个

private void button2_Click(object sender, EventArgs e)
{
    //get all rows in your dataGrid that have a 1 in status_id
    var foundRows = this.dataGridView1.Rows.Cast<DataGridViewRow>().Where(row => row.Cells["status_id"].Value.ToString() == "1");

    //get the count of the found rows
    var count = foundRows.Count(); 

    //in the foundRows search further/deeper for rows that have a 1 in dept_id - this will give rows that have both status_id = 1 AND dept_id = 1
    var foundChildRows = foundRows.Where(row => row.Cells["dept_id"].Value.ToString() == "1");

    //get the count of the found"Child"Rows
    var childCount = foundChildRows.Count();
    this.textBox2.Text = childCount.ToString();
}
private void按钮2\u单击(对象发送者,事件参数e)
{
//获取dataGrid中状态为1的所有行
var foundRows=this.dataGridView1.Rows.Cast().Where(row=>row.Cells[“status\u id”].Value.ToString()=“1”);
//获取找到的行的计数
var count=foundRows.count();
//在foundRows中,进一步/深入搜索部门id为1的行-这将给出状态为1且部门id为1的行
var foundChildRows=foundRows.Where(row=>row.Cells[“dept_id”].Value.ToString()==“1”);
//获取找到的“子”行的计数
var childCount=foundChildRows.Count();
this.textBox2.Text=childCount.ToString();
}

if(dataGridView1.Rows[count-1].Cells[“status\u id”].Value.ToString()==“1”)下面这行代码你想做什么?确切地说,我想知道为什么
[count-1]
?你有以下数据-1,2,2,1,1,然后得到1-so3的计数,现在你得到了计数-1 so3-1=2,得到了索引为2的行,得到了值为2的第三行。-这看起来不是correct@RandRandom仅当对应的值处于status状态时,我才尝试基于部门id列进行计数_id是1我还是一个初学者,但是在一个特定的教程中,它提到了计数-1你认为我应该删除它吗?如果(dataGridView1.Rows[count-1].Cells[“status\u id”].Value.ToString()=“1”),你想知道为什么
[count-1]
?你有以下数据-1,2,2,1,1,然后得到1-so3的计数,现在你得到了计数-1 so3-1=2,得到了索引为2的行,得到了值为2的第三行。-这看起来不是correct@RandRandom仅当对应的值处于status状态时,我才尝试基于部门id列进行计数_id是1我还是一个初学者,但是在一个特定的教程中它提到了计数-1你认为我应该删除它吗?我试图计算有多少行有1如果它有1它显示在一个文本框中的总数,那么我想知道1 from status_id旁边有哪个代码(1,4,5)然后显示在不同的文本框中我试图计算有多少行有1如果有1它显示在文本框中总数,然后我想知道1 from status_id旁边有哪个代码(1,4,5),然后显示在不同的文本框中没问题,很高兴我能帮上忙。没问题,很高兴我能帮上忙。