Search code examples
c#if-statementdatagridviewdatagridviewrow

Removing a row from data grid view


Hi I am trying to remove rows from data grid view on certain conditions. I am successfully deleted rows however this is only working on the 1st condition.

When cell 6 is "" it is removed however when cell 6 is Status it is not. Could you please help please, we the 2nd condition is being ignorned.

       for (int i = dgvAuthG.Rows.Count - 1; i >= 0; i--)
        {
            if ((dgvAuthG.Rows[i].Cells[6].Value == "") || (dgvAuthG.Rows[i].Cells[6].Value == "Status"))
            {
                dgvAuthG.Rows.Remove(dgvAuthG.Rows[i]);
            }
        }
        dgvAuthG.Refresh();

the below is an extract from the debugger

dgvAuthG.Rows[i].Cells[6].Value = "Status"

Solution

  • Having already answered without solving the problem, since then I realised that .Value is an object property not a string, so calling == doesn't work properly as far as I know even if the object is a String (MAYBE).

    Try calling .ToString on .Value before checking equality, or using .Equals(), the latter being better practice in my opinion.