Advertise with us

comparison of datagridview cell values

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.

Last updated:2/4/2014 5:56:00 AM

1 Answers

Pravesh Singh
Pravesh Singh

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

Last updated:2/4/2014 5:56:00 AM