Home > DeveloperSection > Forums > comparison of datagridview cell values
Goti Bandu

Total Post:119

Posted on    February-04-2014 5:30 AM

 C# C# 

 1 Reply(s)
 3820  View(s)
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.

Pravesh Singh

Total Post:412

Posted on    February-04-2014 5:56 AM

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

Don't want to miss updates? Please click the below button!

Follow MindStick