lundi 1 août 2016

Why does my if else statement refuses to read a certain row?

I have a table below that contains the students' results in a DataGridView:-

ID  NAME    RESULT
1   Peter   PASS
1   Peter   SILVER
2   Sam     FAIL
2   Sam     SILVER
3   Simon   FAIL
4   Cliff   PASS
5   Jason   FAIL
5   Jason   FAIL
6   Leonard PASS
6   Leonard FAIL

I'm trying to produce a simple program that will filter out certain rows base on the Results upon a click of a button. What I have achieved right now is that I have able to filter out those who with PASS and/or SILVER as their Result and only display out FAIL.

The problem is right now whenever the button is clicked, it will removed the rows with a PASS and/or SILVER, except the 2nd Row: 1 Peter SILVER. Leaving me with this table below as the end result:-

enter image description here

The only way to resolved this right now is to click the button again.

Below is the source code for the button:-

    private void btnGenerate_Click(object sender, EventArgs e)
    {
       if (dtList.Rows.Count != 0)
       {
           try
           {
               foreach (DataGridViewRow dr in dtList.Rows)
               {
                   //Column names in excel file
                   string colID = dr.Cells["ID"].Value.ToString();
                   string colName = dr.Cells["Name"].Value.ToString();
                   string colResult = dr.Cells["Result"].Value.ToString();


                   if (!colResult.Equals("FAIL", StringComparison.InvariantCultureIgnoreCase))
                   {
                       dtList.Rows.Remove(dr);
                   }

               }
           }
           catch
           {

           }
        }

    }

Aucun commentaire:

Enregistrer un commentaire