comparison of datagridview cell values

Total Post:119


 4616  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.

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


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.