-1

I have a datagrid which returns 4 columns and 244 rows. The 4th column has 3 different statuses "Completed", "In-Progress", "NotSarted" all though out the cells. I need to loop through each cell in the 4th column and read the value add them up and store them in a varibale.So i need to have 3 variables fort he 3 statuses. How do I do ths?

Here is the code that I have. I am not sure if this is the right way to do it or if there is

an easier way, the problem with this is after I run it it keeps telling me "Object reference not set to an instance of an object."

listNames = new List<string>();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
    foreach (DataGridViewCell cell in row.Cells)
    {
        int i = 0;
                if (cell.Value.ToString() == "Completed")
                {
                   i ++;
                   i += i;
                }  
                MessageBox.Show(i.ToString());
        }
    }
}
john
  • 15
  • 4
  • If there is a datasource, you'd be better off working with that – Ňɏssa Pøngjǣrdenlarp Jul 06 '16 at 19:11
  • The cell has obviously no value. Why loop through the cells if you only care about ColumnIndex 3? – LarsTech Jul 06 '16 at 19:11
  • also you declare an `int i` but you never increment it.. I will post something so that you can easily use to follow an perhaps you will understand what's going on by stepping thru the code using the debugger.. – MethodMan Jul 06 '16 at 19:13
  • If the DGV is `AllowUserToAddRows` == true, you are looping thru one row too many as explained in [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it/26761773#26761773) – Ňɏssa Pøngjǣrdenlarp Jul 06 '16 at 19:14
  • @MethodMan Thanks I will be waiting on it – john Jul 06 '16 at 19:15
  • also you should have this outside the for loop ` int i = 0;` and then inside of the for loop last line should be `i++;` if you use the debugger you will see exactly where you are going wrong in regards to comparison use `.Equals when comparing Objects use == when comparing Values` – MethodMan Jul 06 '16 at 19:19

3 Answers3

1

Looks like cell.Value can be null, so replace

if  (cell.Value.Equals("Completed"))

with

if  (cell.Value == "Completed")

Anyway String == operator is overloaded to make comparsion by value so this code will run OK

dimaaan
  • 861
  • 13
  • 16
0

Look at this and test it using the debugger step through the code and understand what's going on with this simple example.

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    foreach (DataGridViewCell cell in row.Cells)
    {
        Console.WriteLine(Convert.ToString(cell.Value));
    }
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • just did it. it is going through each cell row by row. – john Jul 06 '16 at 19:19
  • now take this example and change the inner foreach to fit what it is you're trying to do.. also look at my comment about where to initialize your counter, in your case `i` should be on the outside of the inner foreach loop. – MethodMan Jul 06 '16 at 19:21
  • Ok let me try. Will let you know if it works in a few. Thanks – john Jul 06 '16 at 19:31
  • Well, I used an if ststement to loop through the cells but I can't get the total results for each Status. here is the loop if (cell.Value.ToString() == "Completed") { i ++; i += i; } – john Jul 06 '16 at 20:30
0

If you want to count them, try this

var iRows = dataGridView1.Rows.Cast<DataGridViewRow>();
var statusLookup = iRows.ToLookup(r => r.Cells[3].Value + ""); 
int countOfCompleted = statusLookup["Completed"].Count();
Slai
  • 22,144
  • 5
  • 45
  • 53