0

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"
Carla Fenech
  • 19
  • 2
  • 6
  • You might want to make your comparison case-insensitive, or at least convert to upper or lower. – Trey May 21 '18 at 12:43
  • I cannot see inside your environment live, but what I recommend you do (what I would do) is put a break-point on the line with the if conditions, and do an Add Watch for line portions of the segment that isn't passing so you can figure out what part of it isn't meeting your expectations. Then you will know how to edit it. :) – Hardryv May 21 '18 at 12:45
  • @Hardryv update questions with debugger extract – Carla Fenech May 21 '18 at 12:50
  • you can also convert dgvAuthG.Rows[i].Cells[6].Value to lowercase and then check the condition – Ankita May 21 '18 at 12:52
  • Double check if the value is really "Status" without any blank space before or after the text – Steve May 21 '18 at 12:52

2 Answers2

3

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.

Shefeto
  • 178
  • 1
  • 10
  • 1
    if you think this answer is more useful than your previous one, you may remove it – Amit May 21 '18 at 13:32
  • @Shefeto -- If it is just a question of object type not being a string during the comparison, then the solution for you is simple. .NET includes String as part of Object so it's effectively a global function... on the non-string portion of your compare addend ".ToString()" and that should fix you... problem solved. Sorry for the delay, life keeps me too busy to stay more active on SO nowadays. – Hardryv Jun 12 '18 at 18:10
1

I suggest you print dgvAuthG.Rows[i].Cells[6].Value before the if statement and double check it is definitely "Status", it is case sensitive so "status" will not equate to true.

If you are using visual studio or something I would suggest inserting a break point and using watchers, one for the whole if statement condition, one for the second half of the condition and one for just dgvAuthG.Rows[i].Cells[6].Value to avoid having to print it.

dgvAuthG.Rows[i].Cells[6].Value == "Status" WILL evaluate to true when true, it is just very likely it is not true due to case or something - cannot say more than that with the information that has been given I don't think.

Shefeto
  • 178
  • 1
  • 10
  • Another potential thing to check for is white space. This is really easy to miss, especially if it's a trailing space/tab after the value. – user2366842 May 21 '18 at 12:52
  • Try using .Equals() instead maybe. https://stackoverflow.com/questions/1659097/why-would-you-use-string-equals-over I think .Value is of type object so it may be causing issues. Using .ToString on Value will work too I think, perhaps more reliably. – Shefeto May 21 '18 at 12:56
  • @Shefeto it worked with .Equals!!! thanks mate update your answer so I can mark it – Carla Fenech May 21 '18 at 13:00
  • No problem, I added a new answer since it was so different to my initial answer. – Shefeto May 21 '18 at 13:03
  • @CarlaFenech sorry I forgot to @ you in my last comment, a new answer is up since it was so different, make sure to click the tick as just an upvote doesn't confirm that it solved the problem. – Shefeto May 21 '18 at 13:19