After getting married earlier this year, I really haven't found much time for myself, since adapting to this new married life, really, well, takes time. However, while working on a Visual Basic Web Forms application (don't judge ;)) I found myself strugling at first, making the
GridViewRow.RowState property work. After some time banging my head wondering why it was not working, I realized that it was actually using bit logic and I was looking at this all wrong. Silly me. I had not used bitwise operator in a while and found interesting that I finally find a good use for it. Then I figured this was something worth sharing.
My problem was that I wanted to modify all the rows but the ones marked as
RowState.DataControlRowState.Edit in the RowDataBoud event and at first I was doing it wrong:
If e.Row.RowType = DataControlRowType.DataRow And (e.Row.RowState <> DataControlRowState.Edit) Then ...
It looked fine to me, but obviously it didn't work. An that's how it hit me I had to use bit logic because that is how the RowState property stores its values. The correct way to do this is:
If e.Row.RowType = DataControlRowType.DataRow And ((e.Row.RowState And DataControlRowState.Edit) <> DataControlRowState.Edit) Then ...
By using the AND bitwise operator you are actually checking if the value is in the "bucket" and then comparing the bit value. That is, using an AND on the bucket, would return the bit only if it is in the bucket. It's pretty easy once you understand the concept. I feel I would not do justice explaining in details and providing example, so I would refer you to the link below, which actually covers using bit mask for access control, a pretty common case to use this pattern on:
Hope it helps somebody.