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.
Home / DeveloperSection / Forums / 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.
Pravesh Singh
04-Feb-2014The 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())