COMPARISON OF DATAGRIDVIEW CELL VALUES

Goti Bandu

Total Post:119

Points:835
Posted by  Goti Bandu
C# 
C#
 3982  View(s)
Ratings:
Rate this:

I am trying to compare values of two datagridview cells:

if (kk.BoringData.Rows[rows].Cells[0].Value != kk.BoringData.Rows[rows - 1].Cells[0].Value)

{

}

Both cell values are "B-1", but it returns true.

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: comparison of datagridview cell values

    Hi Goti,


    The Value property is of type object, meaning the != operator tests for reference equality (whether the two objects occupy the same location in memory). To compare the strings by their values you can try using Equals:

    if (!kk.BoringData.Rows[rows].Cells[0].Value.Equals(kk.BoringData.Rows[rows - 1].Cells[0].Value))

    Or convert them to strings before testing them like this:

    if (kk.BoringData.Rows[rows].Cells[0].Value.ToString() != kk.BoringData.Rows[rows - 1].Cells[0].Value.ToString())

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!