count += rowIndex;
Given that
rowIndex
is incremented at each iteration, that means your count will be incremented by 0 the first time, by 1 the second time, by 2 the third time, etc.
Starting from count = 1 : 1, 2, 4, 7, 11, 16, ...
Do you see the problem?
This is not the only issue in your code. For example:
for (int rowIndex = 0; rowIndex < Datagridview1.Rows.Count; rowIndex++)
{
if (Datagridview1.Rows.Count == 0) {
}
}
Since you define rowIndex as strictly less than your dgv's rows count (and since you never delete any row from it either), this part of the code won't ever have any chance to get executed.
You definitely should put a breakpoint at the beginning of your validateMetabolite method, launch the debug mode, and execute line by line, checking at each point what values you get and whether they match your expectations or not. There are too many obvious mistakes, you have to learn to detect them with debugging.
Good luck, you can ask for help on debugging itself, eventually. This is not the less interesting part of development, by far. In fact, this is where you will learn the most.