-3

How do i compare a gridview cell to a string and if they match output it to a variable and then go to the next cell and repeat. This is what i have so far but it doesn't seem to work

private void btnfinalize_Click(object sender, EventArgs e)
{
    for (int i = 0; i < dataGridView1.Rows.Count; ++i)
    {
        if(dataGridView1.Rows[i].Cells[0].Value.ToString() == "Manga vol 1 - 5")
        {
            Global.Book1 = Int32.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
        }
        else if (dataGridView1.Rows[i].Cells[0].Value.ToString() == "Manga vol 6-15")
        {
            Global.Book2 = Int32.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
        }
        else if (dataGridView1.Rows[i].Cells[0].Value.ToString() == "Novels 1-199")
        {
            Global.Book3 = Int32.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
        }
        else if (dataGridView1.Rows[i].Cells[0].Value.ToString() == "Novels 200-400")
        {
            Global.Book4 = Int32.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
        }
        else if (dataGridView1.Rows[i].Cells[0].Value.ToString() == "Comics series mainstream")
        {
            Global.Book5 = Int32.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
        }
        else if (dataGridView1.Rows[i].Cells[0].Value == "Comics series secondary")
        {
            Global.Book6 = Int32.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
        }
        else if (dataGridView1.Rows[i].Cells[0].Value.ToString() == "Text book 1 semester/2 modules")
        {
            Global.Book7 = Int32.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
        }
        else if (dataGridView1.Rows[i].Cells[0].Value.ToString() == "Text book module add-ons")
        {
            Global.Book8 = Int32.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
        }
        else if (dataGridView1.Rows[i].Cells[0].Value.ToString() == "Hardcover")
        {
            Global.Hardcover = Int32.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
        }

    }

i get a System.NullReferenceException: 'Object reference not set to an instance of an object.'

System.Windows.Forms.DataGridViewCell.Value.get returned null.

error

  • 1
    What do you mean by "it doesn't seem to work"? Please could you provide more info? – Jessica Aug 14 '20 at 08:05
  • Could you add more info, such as the wrong output or exception message? Besides, I think `switch statement` is a better choice. – 大陸北方網友 Aug 14 '20 at 08:07
  • i get a System.NullReferenceException: 'Object reference not set to an instance of an object.' System.Windows.Forms.DataGridViewCell.Value.get returned null.erro – ThePersonWhoCodes Aug 14 '20 at 08:36

1 Answers1

0

If you're getting a NullReferenceException, it's probably because you're trying to read the value from a cell that is not in the data.

Are you sure that every row contains at least 2 cells? Currently, your code assumes each row has at least once cell (Cells[0].Value), and depending on the value of the first cell, your code also assumes there's a second cell (Cells[1].Value).

Here's a much better way to approach this:

for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
    var row = dataGridView1.Rows[i];

    if (row.Cells.Count() < 2 || row.Cells[0] == null || row.Cells[1] == null)
    {
        continue; // skips rows that do not have at least 2 cells
    }

    var value = int.Parse(row.Cells[1].Value?.ToString() ?? "0");

    switch (row.Cells[0].Value.ToString())
    {
        case "Manga vol 1 - 5":
            Global.Book1 = value;
            break;
            
        case "Manga vol 6-15":
            Global.Book2 = value;
            break;
            
        case "Novels 1-199":
            Global.Book3 = value;
            break;
            
        case "Novels 200-400":
            Global.Book4 = value;
            break;
            
        case "Comics series mainstream":
            Global.Book5 = value;
            break;
            
        case "Comics series secondary":
            Global.Book6 = value;
            break;
            
        case "Text book 1 semester/2 modules":
            Global.Book7 = value;
            break;
            
        case "Text book module add-ons":
            Global.Book8 = value;
            break;
            
        case "Hardcover":
            Global.Hardcover = value;
            break;
    }
}
Rudey
  • 4,717
  • 4
  • 42
  • 84
  • row.Cells.Count this line doesn't work cause it doesn't contain a definition for it – ThePersonWhoCodes Aug 14 '20 at 12:17
  • If `row.Cells` is an array, use `row.Cells.Length`. If it's a collection, use `row.Cells.Count`. If it's an `IEnumerable`, use `row.Cells.Count()` and add `using System.Linq` near the top of the file. – Rudey Aug 14 '20 at 12:19
  • still get error with this line var value = int.Parse(row.Cells[1].Value.ToString()); – ThePersonWhoCodes Aug 14 '20 at 12:25
  • Well, then your data definitely contains nulls. The real solution here is to find out why your data contains nulls, and fix it from the source. In the meanwhile, try adding `row.Cells[0] == null || row.Cells[1] == null` to the if statement, like I have edited just now in my answer. – Rudey Aug 14 '20 at 12:29
  • even with that line the error still occurs on ```var value = int.Parse(row.Cells[1].Value.ToString());``` – ThePersonWhoCodes Aug 14 '20 at 12:37
  • Again, your data contains nulls, and you should probably figure out why, and fix it at the source. If your data is allowed to contain nulls, and `Value` can be null, you should not call `ToString()` on it. Please familiarize yourself with the concept of `null`, and see https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it#:~:text=Bottom%20Line,else%2C%20null%20gets%20passed%20around. I've updated my answer. – Rudey Aug 14 '20 at 12:51
  • so where would my data contain nulls – ThePersonWhoCodes Aug 14 '20 at 12:58
  • Where does your data come from? A database, a file, from code? Either way, you must be able to view it somewhere. Check if each row contains what you expect, a cell with a string value and a cell with a numerical value. Is one of the numerical values missing from the data? If so, there's your null. You can also put a breakpoint in your code and inspect the value of `dataGridView1.Rows` while debugging. – Rudey Aug 14 '20 at 13:02
  • i ran through the whole code witha breakpoint and couldnt find any nulls, the code loops once and then breaks on the second run – ThePersonWhoCodes Aug 14 '20 at 13:13
  • What's the value of the second cell of the second row? – Rudey Aug 14 '20 at 13:17
  • i fixed up the issue so no more null error but the code still doen't work – ThePersonWhoCodes Aug 14 '20 at 13:28
  • "the code doesn't work" is extremely unhelpful. This will be my last comment. If you're getting an exception, Google it. Every exception occurs for a reason, the only thing you have to do is learn what the exception means. If you're not getting an exception, but it's not working, figure out **what** is not working. If something does not contain the value you expect, figure out why it doesn't by debugging the code. If you don't know how to debug code, there are tutorials for learning about it. – Rudey Aug 14 '20 at 13:36