92AlanC
92AlanC

Reputation: 1387

Calculate number of cells with a specific value

I'm creating a program for test management that has a stats function, which calculates the number of bugs that have been fixed, not fixed or N/A. The test cases are all listed on a DataGridView, where the 1st column is for the test cases, the 2nd one is for the results (the column I'd like to work with) and the latter is just for comments. Here's a bit of my code to show what I'm talking about

private int Passed() // This method is supposed to count how many test cases have passed
    {
        int passed = 0;
        if (/*what condition should I put here?*/) {
            passed++;
        }
        return passed;
    }

    //Is this the best way to display the percentage in real time?
    private void Refresh_Tick(object sender, EventArgs e)
    {
        Display2.Text = Passed().ToString();
    }

The "Results" column cells have each a combobox with its items being "FIXED", "N/A" and "NOT FIXED". Please, I'd like to know how I can programmatically access those cells' value and then use them as a condition to count how many bugs have been fixed.

Upvotes: 0

Views: 798

Answers (1)

Shashank Shekhar
Shashank Shekhar

Reputation: 4178

Iterating through all the rows in the gridview should get you the answer.

int countFixed=0;
int countUnFixed=0;
for(int i=0;i<dgv.RowCount;i++)
{
   if((string)dgv.Rows[i].Cells[1].Value == "Fixed") //try referring to cells by column names and not the index 
      countFixed++;
   else if((string)dgv.Rows[i].Cells[1].Value == "Not Fixed")
      countUnFixed++;
}

Upvotes: 1

Related Questions