Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to compare individual gridview cells and then output the result to a global variable. My code right now has no errors and ive tried running through the code step by step, but it doesn't output anything

What I have tried:

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());
        }

    }
Posted
Updated 14-Aug-20 19:39pm
Comments
F-ES Sitecore 15-Aug-20 10:16am    
If you look at the result you get back from Cells[i].Value you'll see it has spaces, new lines, html etc, it doesn't contain just the text inside. Either compare to the data the grid is bound to, or use .Contains rather than "==", or use a function to strip all leading\trailing text you don't want.

1 solution

I am assuming you have some output related code elsewhere. Current code is just to compare and setup your Global object that seems to have atleast 9 properties.

Now, the string you are comparing might have a word in different case - upper or lower. Try using StringComparison.OrdinalIgnoreCase and see if that goes through.
if (prodCateogry.Trim().Equals("Diamond",StringComparison.OrdinalIgnoreCase))
{

}

Further, use IDE debugger and walk through line by line to see if the object is getting the data assigned if the values compared are found matching (if statements getting executed).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900