djblois
djblois

Reputation: 815

How to test if two cells are equal in excel using closedxml?

I need to test if two cells are equal using closedxml and this is the very basic code that I am using:

if ((PipeSheet.Cell(j, 3).Value == SheetToEdit.Cell(i, RegionCodeInMain).Value))

However, it is not working. I put in a break point with a watch for each side, even when they were equal it was still evaluating to false.

PipeSheet and SheetToEdit along with i and j are variables I set.

What do I need to do differently?

Upvotes: 1

Views: 410

Answers (1)

Raidri
Raidri

Reputation: 17550

The Value property returns an object, which means your program checks for reference equality, not value equality. Depending on the data type for the values use something like this:

if ((PipeSheet.Cell(j, 3).GetValue<int>() == SheetToEdit.Cell(i, RegionCodeInMain).GetValue<int>()))

or

if ((PipeSheet.Cell(j, 3).GetValue<string>() == SheetToEdit.Cell(i, RegionCodeInMain).GetValue<string>()))

Upvotes: 1

Related Questions